15元钱一杯咖啡,喝完后两个空杯换一杯,问:你有100元钱,最多可以喝到几杯咖啡
public static void main(String args[]){
int count = 100/15; //总共能买6杯
int c = cf(count); //喝多多少杯
System.out.println("5元钱一杯咖啡,喝完后两个空杯换一杯,问:你有100元钱,最多喝:"+ c + "杯!");
}
public static int cf(int count) {
int a = 2; //喝完后两个空杯换一杯
int b = count; //空杯子
int c = b;
while (b>1){
if(b%a==0){
c = c + b/a;
b = b/a;
}else{
c = c + (b-1)/a;
b = 1+(b-1)/a;
}
}
return c;
}
答案是:11