package test;
/**
* @author wyl
* @time 2018年8月15日下午6:59:59
*/
public class Drink {
public static void main(String[] args) {
// TODO Auto-generated method stub
int sum=0;
int empty=1;
for(int i=50;i>0;i--){
if (empty!=3) {
empty++;
}else {
empty=1;
i++;
}
sum++;
}
System.out.println(sum);
}
}
思路:
一共有50瓶,
喝完三瓶 + 1
喝完三瓶 + 1
那么 50 - 3 + 1 -3 + 1
以此类推知道。。。最后剩余的可乐数小于3退出。那么依旧题意可以写出
package test;
/**
* @author wyl
* @time 2018年8月15日下午7:12:57
*/
public class Drink1 {
public static void main(String[] args) {
// TODO Auto-generated method stub
int n=50;
int i=0;
while(true){
n-=3;
n++;
i++;
if (n<3) {
System.out.println("共喝了"+(50+i)+"瓶");
break;
}
}
}
}