算法分析
直接模拟
int res = n;
while(n >= 3)
{
瓶: n / 3
盖: n % 3
res += n / 3;
n = n / 3 + n % 3;
}
时间复杂度 $log_3n$
Java 代码
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int res = n;//喝了n瓶
while(n >= 3)
{
res += n / 3;
n = n / 3 + n % 3;
}
System.out.println(res);
}
}