且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

公司今年的一道校招笔试题--五猴分桃

更新时间:2022-09-09 09:56:22

下面是一道公司今年校招的笔试智力题(提前看到的同学如果参加笔试不要说我泄题并答案哈,呵呵)题目如下:

五只猴子分桃。第一只猴子把这堆桃子平均分为五份,多了一个,这只猴子把多的一个吃了,并且拿走了自己的一份。第二只猴子把剩下的桃子又平均分成五份,又多了一个,它同样把多的一个吃了,拿走了自己的一份,第三、第四、第五只猴子都是这样做的,问这堆桃子最少有多少个?

 

对这道题我的解法如下(没作优化)

公司今年的一道校招笔试题--五猴分桃
 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace Test
7 {
8 class Program
9 {
10
11 static void Main(string[] args)
12 {
13 int j = 6;
14 int count = 0;
15 int tmp = 0;
16 while (true)
17 {
18 tmp = j;
19 while ((j - 1) % 5 == 0)
20 {
21
22 count++;
23 j = RemainPeaches(j);
24 if (count == 5)
25 {
26 break;
27 }
28 }
29 if (count == 5)
30 {
31 Console.WriteLine(tmp);
32 break;
33 }
34 tmp++;
35 j=tmp;
36 count = 0;
37 }
38 }
39
40 static int RemainPeaches(int n)
41 {
42 int t = (n-1) - (n - 1) / 5;
43 return t;
44 }
45 }
46 }
公司今年的一道校招笔试题--五猴分桃

运行下就知道结果了,我就不公布了!
当然,如果你有更好的解法,请不要吝啬告诉我哟! 

本文转自Rt-张雪飞博客园博客,原文链接http://www.cnblogs.com/mszhangxuefei/archive/2011/09/29/interests_1.html如需转载请自行联系原作者


张雪飞