题解 | #汽水瓶#
package 华为.模拟;
/*
- 动态规划:
- dp[i]:表示以i个汽水瓶能够换取的汽水个数
- dp[i]=i/3+dp[i/3+i%3]
- 边界条件:dp[1] = 0;dp[2] = 1
- /
import java.util.Scanner;
public class 汽水瓶 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
//输入汽水瓶个数
int n = sc.nextInt();
if (n==0) {
continue;
}
System.out.println(calue(n));
}
}
private static int calue(int n) { int[] dp = new int[n+1]; dp[1] = 0; dp[2] = 1; for (int i = 3; i <= n; i++) { dp[i]=i/3+dp[i/3+i%3]; } return dp[n]; }
}