a,b,c,d,e5个渔夫夜间合伙捕鱼,凌晨时都疲倦不堪,各自在河边的树丛中找地方睡着了。第二天,a第一个醒来,将鱼分成5份,将多余的一条放回河里。b,c,d,e依次醒来,也都同样的办法分鱼,问5个渔夫至少合伙捕了多少条鱼?
我们可以倒过来分析,当最后一个醒来的时候,那么鱼的数量最少应该为5+1条,因此用6作为初始值,然后往上递推4次即可,编写代码如下:
- #include <stdio.h>
-
-
int getfish(int init,int n)
-
{
-
int s = init;
-
while(n){
-
s = 5*s + 1;
-
n--;
-
}
-
-
return s;
-
}
-
-
int main(int argc, char *argv[])
-
{
-
printf("fish which were gotten by fishers at last are %d\n",getfish(6,4));
-
return 0;
-
}
peng@ubuntu:~/src/test/c/suanfa/miaoqu$ gcc 6.11.c
peng@ubuntu:~/src/test/c/suanfa/miaoqu$ ./a.out
fish which were gotten by fishers at last are 3906
阅读(6522) | 评论(6) | 转发(0) |