A、B、C、D、E5个渔夫夜间合伙捕鱼,各自在河边的树丛中休息。待日上三竿,渔夫A第一个醒来,他将鱼分作5份,把多余的一条扔回河中,拿自己的一份回家了。渔夫B第二个醒来,也将鱼分作5份,扔掉多余的一条,拿走自己的一份,接着后三个也按同样的办法分鱼,问5个渔夫至少合伙捕了多少条鱼。
1 #include<iostream> 2 3 using namespace std; 4 5 6 int isInteger(float i) { //用于判断是否是整数 7 if (i - (int)i == 0) { 8 return 1; 9 }10 else {11 return 0;12 }13 }14 15 //从后面往前面推,初始化count表示E醒来看到的条数,故四次循环之后是A看到的条数,16 int fish_count() {17 float count;18 for (int n = 1; n < 10000; n++) {19 count = 5 * n + 1;20 int flag = 1;21 for (int i = 0; i < 4; i++) {22 count = count * 5 / 4 + 1.0;23 if (!isInteger(count)) { 24 flag = 0;25 break;26 }27 }28 if (flag) {29 return (int)count;30 }31 }32 return -1;33 }34 35 int main(int argc, char *argv[]) {36 cout << fish_count();37 getchar();38 return 0;39 }
版权声明
本文仅代表作者观点,不代表百度立场。
本文系作者授权百度百家发表,未经许可,不得转载。



