题目:
有ABCDE五人夜间到河边捕鱼,捕完鱼后五人在河边睡着.
天亮后A先醒来,将所捕鱼平均分伟五份,结果余一条,将余的一条扔掉,带走自己的一堆.B醒来将余下的四堆又分为五份,也余一条,同样仍掉,也带走自己的一堆.C、D、E醒来后也如此,问他们这天晚上至少捕到多少条鱼?
废话不多说,分析:
首先我写了这样一个代码,
//**************************错误示范
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @catch
{
class Program
{
public static int cutfish(int n)
{
if (n == 1)
{
return 6;
}
return cutfish(n - 1) * 5 + 1;
}
// public int count = 1;
static void Main(string[] args)
{
Console.WriteLine(cutfish(5));
}
}
}
//这个结果是 3906 //
//但是看见网上的讨论 最后一个人起来分鱼的时候不一定是每人一条,
就带着这个问题,,我终于想通了,,原来,通过下一次分鱼的总个数算出来的上一次分鱼总个数(设下一次分鱼总个数是为n) 为 :n * 5/4 + 1
那么反过来要把上一次的鱼分为下一次的鱼 那么还要满足上一次的鱼减一 对5个人取余等于零 n * 5/4 % 5 == 0这样就满足所有的条件了,
最后总结一下:
这题目没有给定任何鱼的个数,那么个数可能会有很多种,要求至少,所以在满足上一次 分鱼过后的数目 减掉 4/5 然后 对5 取余 余1(上文讲取余等于0 是因为前提还减掉1 这里没有说减掉1哦 注意);
那么代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace @catch
{
class Program
{
public static bool cutfish(int n, int time) //递归
{
if (time == 5) //分鱼达五次过后 结束递归
{
Console.WriteLine(n); //输出鱼的个数
return true;
}
if (n * 5 / 4 % 5 == 0) //下次的鱼个数是否满足 算出上次以后能够满足分鱼条件
{
// time++;
return cutfish(n * 5 / 4 + 1, ++time); //满足的话 继续往上次分鱼递归
}
return false; //不满足的话就返回false 说明这个 "下次分鱼数目" 假设不成立
}
static void Main(string[] args)
{
int i = 1; //i为假设分鱼数目 假设每人分得一条
bool flag = false; //假设是否成立的标记
while (!flag) //返回true 成立! 则推出执行 不用再继续假设
{
flag = cutfish(i * 5 + 1, 1); //如果返回false则继续执行 并且将最后一次分鱼 每人分得的数量 +1;
i++;
}
}
}
}
//最后正确结果3121
///第一次写,,希望大家有什么意见建议,说出来,反正我又不会高兴接纳的!!!!!! 开个玩笑,一定接纳!!