首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

今天新蛋的笔试题解决思路

2012-01-01 
今天新蛋的笔试题1.20块钱,1块钱1瓶,两个空瓶子可以换一瓶,问最多可以喝几瓶?2.有一名员工发现日历已经7天

今天新蛋的笔试题
1.20块钱,1块钱1瓶,两个空瓶子可以换一瓶,问最多可以喝几瓶?
2.有一名员工发现日历已经7天没有翻了,于是他连着翻了7页,7天的总和刚好是138,问这一天是几号?


[解决办法]
2.闰年2月25号
[解决办法]
1.20+10+5+2+1+1+1=40
[解决办法]

探讨

1.20+10+5+2+1+1+1=40

[解决办法]
2. 3月3日(翻过去 25 + 26 + 27 + 28 + 29 + 1 + 2 = 138)
[解决办法]
1, 可以借瓶子, 40, 不借, 39
有种很好的解释, 就是, 
2个空瓶子换一个, 说明空瓶子的价值是半瓶0.5元, 而水的价值也就是0.5元,
所以 20 % 0.5 = 40 // 最多的时候

2, 
Java code
// 用java写的,// 得出月天数为29天的时候, 才有=138的时候,public class Test138 {    public static void main(String[] args) {        System.out.println(total(28, 7));        System.out.println(total(29, 7));        System.out.println(total(30, 7));        System.out.println(total(31, 7));    }    public static int total(int month, int count) {        int total = 0;        int s = 0;        for (int i = 1; i <= month; i++) {            s = i;            for (int j = 0; j < count; j++) {                if (s > month) {                    s = 1;                }                total += s;                s++;            }            if (total == 138) {                total = i;                break;            } else {                total = 0;            }        }        return total;    }}
[解决办法]
第2题,假设7天都是同一个月那么设经过的第一天为X, 7项等差数列和138,X不能整除,所以应该跨月。
如果跨1天 , 和上面一样,6项等差数列和137 , X也是不能整除。
跨2天 , 5项等差数列和135 , X整除为25 , 那么这一天应该是24号了,而且这个月到29号就跨月了,所以应该是2月24号。
[解决办法]
Java code
1int sum = 20;int bottle = sum;while (bottle > 1) {    sum += bottle/2;    bottle = bottle%2 + bottle/2;}System.out.println(sum);--结果 392int[] end = {28, 29, 30, 31};int days=138, count=0, day, max;boolean found = false;for (int i=0; i<end.length; i++) {    max = end[i] + 7;    day = 1;    while (max > 0) {        count = 0;        for (int j=day; j<day+7; j++) {            if (j > end[i]) {                count += j%end[i];            } else {                count += j;            }        }        if (count == days) {            found = true;            for (int j=day; j<day+7; j++) {                System.out.printf("%d, ", j>end[i] ? j%end[i] : j);            }            break;        }        day++;        max--;    }    if (found) {        System.out.printf("------end=%d\n", end[i]);        break;    }}if (!found) {    System.out.println("error");}
[解决办法]
1.20元能买20瓶水,然后20个瓶子可以换20/2瓶水,20/2个瓶子有可以换20/2/2瓶水,以此类推直到瓶子个数小于2为止。
public class T1{

public static void main(String[] args) {
int sum = 20;
int bottle = sum;
while (bottle > 1) {
sum += bottle/2;
bottle = bottle%2 + bottle/2;
}
System.out.println(sum);
}
}
2.7天加起来等于138,说明这个日期接近月末,所以要考虑这个月共有几天,最常见的是31天,30天,还有二月的28天和闰年二月的29天四个值。
public class T2 {

public static void main(String[] args) {
int[] end = {28, 29, 30, 31};
int days=138, count=0, day, max;
boolean found = false;
for (int i=0; i<end.length; i++) {
max = end[i] + 7;
day = 1;
while (max > 0) {
count = 0;
for (int j=day; j<day+7; j++) {
if (j > end[i]) {


count += j%end[i];
} else {
count += j;
}
}
if (count == days) {
found = true;
for (int j=day; j<day+7; j++) {
System.out.printf("%d, ", j>end[i] ? j%end[i] : j);
}
break;
}
day++;
max--;
}
if (found) {
System.out.printf("------end=%d\n", end[i]);
break;
}
}
if (!found) {
System.out.println("error");
}
}
}
[解决办法]

探讨
引用:

大家的想法都很好,我当时答的是40和3号,是选择题,第一个题我也很纠结,不知道改选39还是40,后来看题目有个最多就选了40

那我不是可以一直借瓶子- -,喝完40 又多出一个空瓶 再借一个。。

[解决办法]
上面修改的程序有误
Java code
int sum = 20;int bottle = sum;while (bottle > 0) { //循环条件,有借瓶则>0,无借瓶则>1    sum += (bottle == 1 ? 1 : bottle/2);    bottle = bottle%2 + (bottle == 1 ? -1 : bottle/2); //借瓶-1}System.out.println(sum);
[解决办法]
探讨
这样的话可以一直借下去啊!
30楼说的:
最后一次你就两个空瓶全部交还给别人了,没有空瓶了

最后2个空瓶子还能换一瓶啊!这样下去,死循环了...

[解决办法]
40瓶
[解决办法]
探讨
第2题,假设7天都是同一个月那么设经过的第一天为X, 7项等差数列和138,X不能整除,所以应该跨月。
如果跨1天 , 和上面一样,6项等差数列和137 , X也是不能整除。
跨2天 , 5项等差数列和135 , X整除为25 , 那么这一天应该是24号了,而且这个月到29号就跨月了,所以应该是2月24号。

[解决办法]
[code=Java]
int k = 20;
for(int i=1;i <=k;){
System.out.println( "already drank[ "+i+ "]bottles ");
if(i%2 == 0){
k++;
}
i++;
}
[/code]

热点排行