递归算法的流程图表示(c语言课程设计:选出一位幸运人士,一定要用递归算法!要源代码,流程图,和算法描述!)

本文目录
c语言课程设计:选出一位幸运人士,一定要用递归算法!要源代码,流程图,和算法描述!
这道题做ACM题目的时候做过,当时使用数组做的,最后因为效率太低通过不了。
G Josephus Problem
Time Limit:1000MS Memory Limit:65535K
题型: 编程题 语言: 无限制
描述
Josephus Problem is an ancient problem named for Flavius Josephus. There are people standing in a circle waiting to be executed. The counting out begins at the
first point in the circle and proceeds around the circle in a fixed direction. In each step, one person is skipped and the next person is executed. The elimination
proceeds around the circle (which is becoming smaller and smaller as the executed people are removed), until only the last person remains, who is given freedom.
For example, if there are 10 people in the circle, the executed order is 2, 4, 6, 8, 10, 3, 7, 1, 9. So the 5th person survives.
Now we define a function J(n) to be the survivor’s number when there are n person in the circle, and J^2(n)=J(J(n)), for instance J^2(10)=J(J(10))=J(5)=3,
and J^3(n)=J(J(J(n))), and so on. Could you calculate J^m(n)?
输入格式
The input consists of a number of cases.
Each case contains integers n and m. 0《n, m《=10^9
The input is terminated by a case with m=n=0
输出格式
For each case, print the number J^m(n)
输入样例
10 2
10 1
20 1
0 0
输出样例
3
5
9
Provider
admin
#include《stdio.h》
#include《malloc.h》
#include《math.h》
int J(int number,int * circle)
{
int i,length=number;
for(i=0;i《number;i++)
{
circle=i+1;
}
while(length》1)
{
if(length%2==0)
{
for(i=0;i《length;i++)
{
circle;
}
length=length/2;
continue;
}
if(length%2==1)
{
circle;
for(i=1;i《length;i++)
{
circle;
}
length=length/2+1;
continue;
}
}
return circle;
}
int main()
{
int n,m,i,*circle;
circle=(int*)malloc(n*sizeof(int));
while(1){
{
scanf("%d%d",&n,&m);
}while(n《0||m《0||m》10);
if(n==0&&m==0)
break;
for(i=0;i《m;i++)
{
n=J(n,circle);
}
printf("%d\n",n);
}
return 0;
}
C语言猴子吃桃问题流程图
一个猴子摘了一些桃子,它每天吃了其中的一半然后再多吃了一个,
直到第10天,它发现只有1个桃子了,问它第一天摘了多少个桃子?
猴子分N天吃完了桃子,要想求出第1天的桃子数,就先要求出第2天的桃子数,.......因此,有:
a1=(a2+1)*2;
a2=(a3+1)*2;
a3=(a4+1)*2;
......
a9=(a10+1)*2;
a10=1;
现在就知道了算法,我们可以用递归来求解:
int qiu(int a,int n)
{
if(n==1) a=1; //第10天就只剩1个了
else a=(a(n-1)+1)*2; //前一天总比后1天多一半加1
}
-------------------------------------
#include《stdio.h》
int qiu(int a,int n);
main(){
int zuih=1,tians=10;//最后一天的个数,天数
long sum;
sum=qiu(1,10);
printf("di yi tian you %ld ge.\n"):
}
int qiu(int a,int n)
{
if(n==1) a=1; //第10天就只剩1个了
else a=(a(n-1)+1)*2; //前一天总比后1天多一半加1
}
描述算法的常用方法
1.什么是算法
从字面上来说,算法也就是用于计算的方法。是用来解决某些问题的方法。通过这个方法,可以达到想要的计算结果。它就像我们小时候学些的一些数学公式和解题步骤。
算法,一般有5个特征:
有穷性:
算法的执行步骤、时间、都是有限的。不会无休止的一直执行下去。
确切性:
算法的每一步都必须有明确的定义和描述。
输入:
一个算法应该有相应的输入条件,就像我们小时候做的应用题,已知什么什么。来求某个结果,已知部分便是输入条件。
输出:
算法必须有明确的结果输出。没有结果,那这个算法是没有任何意义的。
可行性:
算法的步骤必须是可行的,无法执行的则没有意义,也解决不了任何问题
2.算法的分类
按照算法的应用来分:算法可以分为基本算法、几何算法、加密/解密算法、查找算法、图标数据分析算法等。
按照算法的思路来分:算法可以分为递推算法、递归算法、穷举算法、分治算法等。
下面,我们就来讲我们的重点之一:也就是算法思想:
3.常用算法思想
穷举算法思想;
递推算法思想;
递归算法思想;
分治算法思想;
概率算法思想;

更多文章:
service pack 3(操作系统版本升级(SP) Service Pack 3当中的“Service Pack 3”是什么意思)
2026年9月22日 10:20
html代码怎么写大佬教程(html网页的题来个大佬,写代码,题目在图上)
2026年9月22日 10:10
结构体内又一个struct(c++ 在结构体中再嵌入一个结构体如何调用)
2026年9月22日 09:40
cocos creator中文(cocoscreator和cocoscreator3d的区别)
2026年9月22日 02:30





