Scanner sc = new Scanner(System.in); //创建键盘录入对象
System.out.println("请输入人数:");
int i = sc.nextInt();
int num = methord(i); // 调用计算方法
System.out.println(i + "个人最少需要买" + num + "瓶饮料");
}
public static int methord(int i) {
int a = 1; // 用来循环/3,%3,的过程
int b = 0; // 用来记录共喝了多少瓶
int c = 0; // 用来记录a的原始值
while (true) { // a从1开始不断自增,当满足人数时,返回该值
c = a;
b = 0; // 每次循环对b值进行还原。
b = b + a;
while (a / 3 > 0) {
b = b + a / 3; // 瓶盖换的汽水数
a = a / 3 + a % 3; // 换完后剩余的瓶盖数
}
// if(a%3==2)当可以先喝汽水再用三个瓶盖换时,加入这两步
// b++;
if (b >= i)
return c;
else
a = c + 1;
}
}