zoukankan      html  css  js  c++  java
  • 分鱼问题

    A、B、C、D、E五个人在某天夜里合伙去捕鱼,到第二天凌晨时都疲惫不堪,于是各自找地方睡觉。日上三杆,A第一个醒来,他将鱼分为五份,把多余的一条鱼扔掉,拿走自己的一份。B第二个醒来,也将鱼分为五份,把多余的一条鱼扔掉,保持走自己的一份。C、D、E依次醒来,也按同样的方法拿走鱼。问他们合伙至少捕了多少条鱼?

     1 #include <iostream>
     2 using namespace std;
     3 
     4 bool fun1(int n, int count)
     5 {
     6     if (0 == count)
     7     {
     8         return false;
     9     }
    10     if (0 == n)
    11     {
    12         return true;
    13     }
    14     if (1 != (count % 5))
    15     {
    16         return false;
    17     }
    18     return fun1(n - 1, count / 5 * 4);
    19 
    20 }
    21 int fun2(int n)
    22 {
    23     if (n == 1)
    24     {
    25         static int i = 0;
    26         do
    27         {
    28             i++;
    29         }
    30         while (i % 5 != 1);
    31         return i;
    32     }
    33     else
    34     {
    35         int t = 0;
    36         do
    37         {
    38             t = fun2(n - 1);
    39         }
    40         while (t % 4 != 0);
    41         return (t / 4 * 5 + 1) ;
    42     }
    43 }
    44 int fun3(void)
    45 {
    46     bool flag = false;
    47     for (int n = 6; ; n += 5)
    48     {
    49         int temp = n;
    50         flag = true;
    51         for (int j = 5; j > 0; --j)
    52         {
    53             if (temp % 5 != 1)
    54             {
    55                 flag = false;
    56                 break;
    57             }
    58             temp = temp / 5 * 4;
    59         }
    60         if (flag)
    61         {
    62             return n;
    63         }
    64     }
    65 }
    66 int main()
    67 {
    68     for (int n = 0; true; ++n)
    69     {
    70         if (fun1(5, n))
    71         {
    72             cout << n << endl;
    73             break;
    74         }
    75     }
    76 
    77     cout << fun2(5) << endl;
    78     cout << fun3() << endl;
    79 
    80     return 0;
    81 }

    输出是:

    3121
    3121
    3121
    
    Process returned 0 (0x0)   execution time : 0.807 s
    Press any key to continue.
  • 相关阅读:
    ExecuteNonQuery()返回值
    GridView导入至EXCEL (报错处理:只能在执行 Render() 的过程中调用 RegisterForEventValidation)
    mysql 远程登录
    四舍六入 银行家算法
    linux-grep-tail-find
    spring 事务注解
    aop execution 表达式解析
    事务有效条件
    oracle 日期取 月 日
    spring cloud 定时任务
  • 原文地址:https://www.cnblogs.com/xkxjy/p/3712563.html
Copyright © 2011-2022 走看看