日期:2014-05-20 浏览次数:20713 次
// 用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号。
------解决方案--------------------
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]) {