一道关于Java及智力测试的面试题!解决思路
一道关于Java及智力测试的面试题!
情况是这样的:每3个空瓶换一瓶矿泉水.请问27个人,每个人都要喝到水,最少要买多少瓶矿泉水!!!!
我是菜鸟!我是这么做的
public class bottle {
public static void main(String[] args) {
int temp=0,i;
for(i=0;i<27;i++)
if((i+i/3)>=27){
temp=i;
break;}
// TODO, add your application code
System.out.println(temp);
}
}
------解决方案--------------------
i = 4分之81即21瓶最少
但是。。。
你这样算是错的- -|、
------解决方案--------------------
情况是这样的:每3个空瓶换一瓶矿泉水.请问27个人,每个人都要喝到水,最少要买多少瓶矿泉水!!!!
我是菜鸟!我是这么做的
public class bottle {
public static void main(String[] args) {
int temp=0,i;
for(i=0;i<27;i++)
if((i+i/3)>=27){
temp=i;
break;}
// TODO, add your application code
System.out.println(temp);
}
}
------解决方案--------------------
i = 4分之81即21瓶最少
但是。。。
你这样算是错的- -|、
------解决方案--------------------
- Java code
public class T { public static void main(String[] args) { for (int x = 0; x < 10000; x++) { if (x + suan(x,3) >= 27) { System.out.println(x); break; } } } public static int suan(int x,int ping) { if (x < ping) { return 0; } int xx = Math.round(x / ping); int yushu = x - xx * ping; return xx + suan(xx + yushu,ping); } }
------解决方案--------------------
呵呵。这是一道笔试题,华为终端今年考的笔试题,只知道正确答案是18瓶,27/3*2
推荐:http://topic.****.net/u/20080916/22/dd7b1070-eebe-4c2d-9e6f-c48e385777e5.html?1463872763
------解决方案--------------------
好像我智力也不行了。。
------解决方案--------------------
ding
------解决方案--------------------
的确是18。。。