问题:
5个人夜间捕鱼,早上A先醒来,将鱼分为5份,将多的一条扔进海里,然后B醒来,不知道A已经拿走一份鱼,就将剩下的鱼分成5份,扔掉多余的一条,接着C,D,E醒来,按同样的方法分鱼,问这5个人合伙捕到多少条鱼,每个人醒来后所看到多少条鱼。
#include <stdio.h>
#include <stdlib.h>
int fish(int n, int x);
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char *argv[]) {
int i=0, flag = 0, x;
do{
i=i+1;
x=i*5+1;
if(fish(5,x))//递归判断
{
flag = 1; //flag标识
printf("五个人合伙捕到的鱼总数是%d\n", x);
}
}while(!flag);
return 0;
}
int fish(int n, int x)//x表示人数,x表示醒来后剩下的鱼
{
if(x%5==1)
{
if(n==1)
return 1;
else
return fish(n-1, (x-1)/5*4);
}
return 0;
}//这里递归的作用是作为判断条件
原文地址:http://blog.csdn.net/orangeisnotapple/article/details/44871627