今天新蛋的笔试题解决思路
今天新蛋的笔试题
1.20块钱,1块钱1瓶,两个空瓶子可以换一瓶,问最多可以喝几瓶?
2.有一名员工发现日历已经7天没有翻了,于是他连着翻了7页,7天的总和刚好是138,问这一天是几号?
------解决方案--------------------
2.闰年2月25号
------解决方案--------------------
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,
1.20块钱,1块钱1瓶,两个空瓶子可以换一瓶,问最多可以喝几瓶?
2.有一名员工发现日历已经7天没有翻了,于是他连着翻了7页,7天的总和刚好是138,问这一天是几号?
------解决方案--------------------
2.闰年2月25号
------解决方案--------------------
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
1 int sum = 20; int bottle = sum; while (bottle > 1) { sum += bottle/2; bottle = bottle%2 + bottle/2; } System.out.println(sum); --结果 39 2 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"); }
------解决方案--------------------
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]) {