创新工厂面试题详解:共打了多少鱼
来源:互联网 发布:三菱plc触摸屏编程视频 编辑:程序博客网 时间:2024/06/10 04:31
最近看到一个创新工厂的面试题,很有意思,下面给出算法实现(Java代码)。如果哪位有更好的算法,请跟贴。
abcde五人打渔,打完睡觉,a先醒来,扔掉1条鱼,把剩下的分成5分,拿一份走了;b再醒来,也扔掉1条,把剩下的分成5份,拿一份走了;然后cde都按上面的方法取鱼。问他们一共打了多少条鱼,写程序和算法
共打了多少条鱼的结果有很多。但求最少打的鱼的结果是3121条鱼(应该找这5个人问问,用什么工具打了这么多条鱼)
大家可以先用计算器验证一下3121是否正确。
a开始拿鱼: (3121 - 1) / 5 = 625
同理,bcde分别获得的鱼数(不包括其扔掉的鱼)b:499 c:399 d:319 e:255
这道题最简单的方法就是枚举。从最小值开始,先看看代码(Java实现)。
public class Test{public static void main(String[] args){// 分别保存a至e获取的鱼数(为了方便,包括其扔掉的鱼)int[] everybody_fish = new int[5];// 临时数组,保存当前鱼数减1后除5的余数,只有都为0,才满足条件int[] temp = new int[5];// 从1扫描到10000for (int x = 1; x <= 10000; x++){// 当前已被取走多少鱼(包括被扔的鱼)int sum = 0;int i = 0;// 计算abcde各获取的鱼数for (i = 0; i < everybody_fish.length; i++){temp[i] = (x - 1 - sum) % everybody_fish.length;// 只要有一个人不能平均分配剩余的鱼,就不满足条件if (temp[i] != 0)break;everybody_fish[i] = (x - 1 - sum) / everybody_fish.length + 1;sum += everybody_fish[i];}// for循环正党结束,满足条件,输出相应的值。if (i == everybody_fish.length){System.out.print("共钓了" + x + "条鱼 ");for (i = 0; i < everybody_fish.length; i++){System.out.print((char) ('a' + i) + ":"+ (everybody_fish[i] - 1) + " ");}System.out.print("最后剩余" + (x - sum) + "条鱼 ");System.out.println("扔了" + everybody_fish.length + "条鱼");}}}}
运行上面的代码,会输出如下三行信息
共钓了3121条鱼 a:624 b:499 c:399 d:319 e:255 最后剩余1020条鱼 扔了5条鱼
共钓了6246条鱼 a:1249 b:999 c:799 d:639 e:511 最后剩余2044条鱼 扔了5条鱼
共钓了9371条鱼 a:1874 b:1499 c:1199 d:959 e:767 最后剩余3068条鱼 扔了5条鱼
在10000以内只有三个数满足这个条件。上面的代码是通用的,如将两个数组的长度改为6,将10000改成50000,会输出如下信息。
共钓了46651条鱼 a:7775 b:6479 c:5399 d:4499 e:3749 f:3124 最后剩余15620条鱼 扔了6条鱼
也就是说一共6个人。每个人也是先扔一条鱼,然后再将剩余的鱼6等分,取走一份。 在50000以内只有46651满足这个条件。
在本题中只有如下的代码是核心算法,其他的都是枚举和输出结果的代码。
for (i = 0; i < everybody_fish.length; i++)
{
temp[i] = (x -1 - sum) % everybody_fish.length;
// 只要有一个人不能平均分配剩余的鱼,就不满足条件
if (temp[i] !=0)
break;
everybody_fish[i] = (x - 1 - sum) / everybody_fish.length + 1;
sum +=everybody_fish[i];
}
下面的是跟帖回复的内容
Ruby 如下:
1000.upto(50000) do |sum|
times=0
temp=sum
5.times do
break if((temp-1)%5!=0)
temp = temp -(temp-1)/5 - 1
times += 1
if times==5
puts "The total number of fish is: "+sum.to_s
end
end
times=0
end
结果:
The total number of fish is: 3121
The total number of fish is: 6246
The total number of fish is: 9371
The total number of fish is: 12496
The total number of fish is: 15621
The total number of fish is: 18746
The total number of fish is: 21871
The total number of fish is: 24996
The total number of fish is: 28121
The total number of fish is: 31246
The total number of fish is: 34371
The total number of fish is: 37496
The total number of fish is: 40621
The total number of fish is: 43746
The total number of fish is: 46871
The total number of fish is: 49996
----------------------------------------------------------------------------------------------------
int x=1;
boolean flag = true;
for (; x < 10000; x++) {
float fashGet;
float sum = 0;
for (int i = 0; i < 5; i++) {
fashGet = (x - sum - 1) / 5;
sum += fashGet + 1;
if (fashGet % 2 == 0 || (fashGet + 1) % 2 == 0) {
flag = true;
} else {
flag = false;
break;
}
}
if (flag == true) {
System.out.println("捕到的鱼数是" + x);
break;
}
}
------------------------------------------------------------------------------------------
- #include "stdio.h"
- #include <math.h>
- #include <ctype.h>
- /* 核心算法 */
- int Fish_amount_speed(int p_num)
- {
- int n = p_num ;
- int i_up = 0;
- int i_up_1 = pow(5.0,n);
- int i_up_2 = pow(4,(n+1));
- int i_down = pow(4.0,n);
- for (int i = 1;1;i++)//i 已经是p0值,所以要计算的 人数里面 共有 p_num -1个,正好是结果需要值
- {
- i_up = (i+4)*i_up_1 + i_up_2;
- if(i_up%i_down == 0)return (i_up/i_down);
- }
- return 0;
- }
- int main()
- {
- int people_max = 5;
- int fish_get_last = 1;
- printf("最少是%d条\n",Fish_amount_speed(people_max));
- for (int i = 0;i < people_max;i++)
- {
- printf("第%d个人拿走了
- %d条\n",i+1,((fish_cnt_min-1)/5));
- fish_cnt_min = (fish_cnt_min-1)/5 * 4;
- }
- return 0;
- }
结果输出:
最少是3129条
第1个人拿走了 625条
第2个人拿走了 499条
第3个人拿走了 399条
第4个人拿走了 319条
第5个人拿走了 255条
------------------------------------------------------------------------------------------------------------------
- int []res= newint[5];
- int count = 1;
- for(int i =5;i<=10000;i++){
- int temp_var = 0;
- for(int j =0;j<5; j++){
- if(j == 0){
- if( (i-1)*4%5 ==0 ){
- temp_var = (i-1)*4/5;
- res[j] = i - 1 - temp_var;
- }else{
- break;
- }
- }else{
- if( (temp_var-1)*4%5 ==0 ){
- int temp_var1 = (temp_var-1)*4/5;
- res[j] = temp_var - 1 - temp_var1;
- temp_var = temp_var1;
- }else{
- break;
- }
- }
- if(j == 4){
- System.out.println("************答案"+(count++)+"************");
- System.out.println("共钓到鱼的总数:"+i+",剩余鱼的总数:"+temp_var);
- for(int p =0; p<5 ;p++){
- System.out.println("第"+(p+1)+"个人,拿走了"+res[p]+"条鱼");
- }
- }
- }
- }
--------------------------------------------------------------------------------------------------------------
- int CalcFishes(int numFishers)
- {
- int i = 0;
- int start = 7, totalFishes;
- totalFishes = start;
- while(i < numFishers)
- {
- if((totalFishes+1) % 4 == 0)
- {
- totalFishes = (totalFishes+1)/4 * 5;
- ++i;
- }
- else
- {
- start += 4;
- totalFishes = start;
- i = 0;
- }
- }
- return totalFishes+1;
- }
- int main(void)
- {
- printf("Total fishes: %d\n", CalcFishes(5));
- return 0;
- }
还有好多回复以及原文点击下面的链接
原文链接
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼
- 也谈 创新工厂面试题详解:共打了多少鱼
- 创新工厂面试题详解:共打了多少鱼 -- 正解 递归求解法
- 一道创新工场面试题详解:共打了多少鱼?
- 算法编程:共打了多少鱼?
- 创新工厂面试题
- 创新工厂的面试题
- 创新工厂面试题详解 我的代码
- [转]创新工厂的面试题
- 听说是“创新工厂”的一道面试题
- 创新工场面试题
- 完全平方数和问题(创新工厂涂鸦移动面试题)
- 联想万全慧眼V专业版监控管理系统详解
- Vmware 虚拟机和主机的链接问题
- 正则表达式在EditPlus中的常见应用
- Struts2输入校验(四) の校验框架 ——非字段校验
- swing:为什么一个类里调用另一个类的panel不显示
- 创新工厂面试题详解:共打了多少鱼
- Oracle datafile block 格式 说明
- PHP学习提纲
- ubuntu wine 中文软件界面乱码解决方法
- 众多Android 开源项目推荐,给力工作给力学习
- Oracle 手工清除回滚段的几种方法
- 在word中添加并显示一个ActiveX控件,vc代码,一直不成功,请教
- c语言中的sizeof(面试、笔试常考)
- ORA-600 [2662] Block SCN is ahead of Current SCN 处理方法 说明