题目:
现有×瓶啤酒,每3个空瓶子换一瓶啤酒,每7个瓶盖子也可以换一瓶啤酒,问最后可以喝多少瓶啤酒。
这道题, 一开始我完全没思路😭, 听了王争老师讲解, 才有了下面的思路
思路:
java实现
public int drink(int x) {
int count =x, bottle =x, cap=x;
while (bottle >=3 || cap >= 7) {
if(bottle>= 3) {
bottle = bottle-3;
count++;
bottle++;
cap++;
}
if(cap >=7) {
cap = cap - 7;
count++;
bottle++;
cap++;
}
}
return count;
}
王争老师的解法
public int drink(int x) {
int count =x, bottle =x, cap=x;
while (bottle >=3) {
while (bottle>=3) {
int change = bottle/3;
count +=change;
bottle%=3;
bottle+=change;
cap+=change;
}
while (cap>=7) {
int change = cap/7;
count +=change;
cap%=7;
bottle+=change;
cap+=change;
}
}
return count;
}