能借瓶子的话,最后一次你就两个空瓶全部交还给别人了,没有空瓶了 比如最后你有1个瓶子,你借1瓶,喝完后2个瓶子都要给人,这是不再剩瓶子了,所以不会一直借的 如果可以借瓶子,那么修改一下程序 int sum = 20; int bottle = sum; while (bottle > 0) { //修改这里就可以了,循环直到没有瓶子为止,不能借的话,最后剩1个瓶子 sum += bottle/2; bottle = bottle%2 + bottle/2; } System.out.println(sum);
上面修改的程序有误 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);
int k = 20; for(int i=1;i<=k;){ System.out.println("already drank["+i+"]bottles"); if(i%2 == 0){ k++; } i++; }
int _tmain(int argc, _TCHAR* argv[]) { int nDateAcross; for ( int nDateAcross = 0; nDateAcross < 7; nDateAcross++ ) { int sum = 0; for ( int j = 0; j <= nDateAcross; j++ ) sum =sum + j; int y = ( 138 - sum ) * 2; if ( y%(7-nDateAcross) == 0 ) { int Ans = y/(7-nDateAcross); if ( (Ans - (6 -nDateAcross))%2 == 0 ) { int date = ( Ans - ( 6-nDateAcross ) ) / 2; if ( date >= 1 && date <= 31 ) { printf("The beginning date: %d\n",date - 1); printf("Today: %d\n",nDateAcross + 1); } else printf("The wrong date: %d\n",date - 1); } } } getchar(); return 0; }根据公式: sun(x)=(1+x)/2*x; ( x + (x + 6 - nDateAcross) )/2 * (7 - nDateAcross) = (138 - sum(nDateAcross));
;WITH MU AS ( SELECT GETDATE() AS COL1,1 AS NUM UNION ALL SELECT DATEADD(DAY,1,COL1) ,NUM+1 FROM MU WHERE DATEADD(DAY,1,COL1)<DATEADD(YEAR,1,GETDATE()) )SELECT T1.COL1,DAY(T1.COL1),DAY(T2.COL1),DAY(T3.COL1),DAY(T4.COL1),DAY(T5.COL1),DAY(T6.COL1),DAY(T7.COL1) FROM MU T1 INNER JOIN MU T2 ON T1.NUM=T2.NUM-1 INNER JOIN MU T3 ON T2.NUM=T3.NUM-1 INNER JOIN MU T4 ON T3.NUM=T4.NUM-1 INNER JOIN MU T5 ON T4.NUM=T5.NUM-1 INNER JOIN MU T6 ON T5.NUM=T6.NUM-1 INNER JOIN MU T7 ON T6.NUM=T7.NUM-1 WHERE DAY(T1.COL1)+DAY(T2.COL1)+DAY(T3.COL1)+DAY(T4.COL1)+DAY(T5.COL1)+DAY(T6.COL1)+DAY(T7.COL1)=138 OPTION(MAXRECURSION 500)
有种很好的解释, 就是,
2个空瓶子换一个, 说明空瓶子的价值是半瓶0.5元, 而水的价值也就是0.5元,
所以 20 % 0.5 = 40 // 最多的时候2, // 用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;
}
}
如果跨1天 , 和上面一样,6项等差数列和137 , X也是不能整除。
跨2天 , 5项等差数列和135 , X整除为25 , 那么这一天应该是24号了,而且这个月到29号就跨月了,所以应该是2月24号。
int sum = 20;
int bottle = sum;
while (bottle > 1) {
sum += bottle/2;
bottle = bottle%2 + bottle/2;
}
System.out.println(sum);
--结果 392
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");
}
--结果 25, 26, 27, 28, 29, 1, 2, ------end=29
System.out.println(day(138,7,0));
}
public static int day(int sum,int n,int d) {
int test = n*(n-1)/2;
int test2 = (sum - test)%n;
if(test2==0) {
return (sum - test)/n;
}
else {
return day(sum-d-1,n-1,d+1);
}
}
结果25如果总天数是91public static void main(String[] args) {
System.out.println(day(91,7,0));
}
结果为10算法没错了。
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");
}
}
}
*瓶子的自己看法
*/
public class Run { public static void main(String[] args) throws Throwable{
int a = drink(20);
System.out.println(a);
} /*
* 使用递归思想,n代表拥有几瓶水。
* 每次喝两瓶,并拿空瓶换一瓶水,这样就喝了2瓶水 ,还有n-2瓶没喝,于是用两个空瓶换一瓶水,
* 这样就有n-1瓶没喝。
*/
public static int drink(int n){
if(n==1)
return 1; //如果只有一瓶水了,不可以借瓶子,就只能喝这一瓶 return 1,如果可以借,就喝两瓶,return 2;
return 2 + drink(n - 1);
}
}
public static void main(String[] args)就行
第一题是“给50瓶可乐,每喝3瓶,用这3个空瓶可以再换一瓶,问共可以喝多少瓶?”的变种。题目还是不错。用java写出来的,更好!
比如最后你有1个瓶子,你借1瓶,喝完后2个瓶子都要给人,这是不再剩瓶子了,所以不会一直借的
如果可以借瓶子,那么修改一下程序
int sum = 20;
int bottle = sum;
while (bottle > 0) { //修改这里就可以了,循环直到没有瓶子为止,不能借的话,最后剩1个瓶子
sum += bottle/2;
bottle = bottle%2 + bottle/2;
}
System.out.println(sum);
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);
int k = 20;
for(int i=1;i<=k;){
System.out.println("already drank["+i+"]bottles");
if(i%2 == 0){
k++;
}
i++;
}
int _tmain(int argc, _TCHAR* argv[])
{
int nDateAcross;
for ( int nDateAcross = 0; nDateAcross < 7; nDateAcross++ )
{
int sum = 0;
for ( int j = 0; j <= nDateAcross; j++ )
sum =sum + j;
int y = ( 138 - sum ) * 2;
if ( y%(7-nDateAcross) == 0 )
{
int Ans = y/(7-nDateAcross);
if ( (Ans - (6 -nDateAcross))%2 == 0 )
{
int date = ( Ans - ( 6-nDateAcross ) ) / 2; if ( date >= 1 && date <= 31 )
{
printf("The beginning date: %d\n",date - 1);
printf("Today: %d\n",nDateAcross + 1);
}
else
printf("The wrong date: %d\n",date - 1);
}
}
}
getchar();
return 0;
}根据公式:
sun(x)=(1+x)/2*x;
( x + (x + 6 - nDateAcross) )/2 * (7 - nDateAcross) = (138 - sum(nDateAcross));
SELECT GETDATE() AS COL1,1 AS NUM
UNION ALL
SELECT DATEADD(DAY,1,COL1) ,NUM+1
FROM MU
WHERE DATEADD(DAY,1,COL1)<DATEADD(YEAR,1,GETDATE())
)SELECT
T1.COL1,DAY(T1.COL1),DAY(T2.COL1),DAY(T3.COL1),DAY(T4.COL1),DAY(T5.COL1),DAY(T6.COL1),DAY(T7.COL1)
FROM MU T1
INNER JOIN MU T2 ON T1.NUM=T2.NUM-1
INNER JOIN MU T3 ON T2.NUM=T3.NUM-1
INNER JOIN MU T4 ON T3.NUM=T4.NUM-1
INNER JOIN MU T5 ON T4.NUM=T5.NUM-1
INNER JOIN MU T6 ON T5.NUM=T6.NUM-1
INNER JOIN MU T7 ON T6.NUM=T7.NUM-1
WHERE DAY(T1.COL1)+DAY(T2.COL1)+DAY(T3.COL1)+DAY(T4.COL1)+DAY(T5.COL1)+DAY(T6.COL1)+DAY(T7.COL1)=138
OPTION(MAXRECURSION 500)