MOOC清华《程序设计基础》第6章:分鱼问题(不用数组递推)

来源:互联网 发布:tensorflow lite官网 编辑:程序博客网 时间:2024/05/16 13:56

#include <iostream>  using namespace std;    int main()  {       int num;for(num = 16;  ; num += 20)      {           int i;  //int before;  //之前醒来的那个人看到的鱼数 int now = num;  //现在这个人看到的鱼数        for(i = 4 ; i >= 1; i--)          {  if(now % 4 != 0)                  break;              now = now / 4 * 5 + 1;//before = now / 4 * 5 + 1; //now = before;         }  //其实可以把before省去,但是为了程序逻辑清晰,留着为好         if(i >= 1)              continue;          num = now;  //注意一定要把最后的now值传递给num,而不是最后的num值         break;            }              //num = 3121;  //测试代码,测试输出模块是否正确     for(int i = 0; i < 5; i++)      {  cout << char(i + 65) << " 看到的鱼数是 " << num << "条" << endl;          cout << char(i + 65) << " 分到了 " << (num - 1) / 5 << " 条鱼!" << endl;          num = (num - 1) / 5 * 4;        cout << endl;      }            return 0;  }  




阅读全文
0 0
原创粉丝点击