解题思路: 动态规划模板题了,不过我的方式略微有一点不同,思路都差不多。
参考代码:
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] dp = new int[10000000]; int[] ar = new int[2]; ar[0] = sc.nextInt(); ar[1] = sc.nextInt(); dp[0] = 0; int max = 0; int count = 0; for (int i = 1; i < dp.length; i++) { dp[i] = Integer.MAX_VALUE; for (int j = 0; j < ar.length; j++) { if (i >= ar[j] && dp[i - ar[j]] != Integer.MAX_VALUE) { dp[i] = Math.min(dp[i - ar[j]] + 1, dp[i]); } } if (dp[i] == Integer.MAX_VALUE){ max = i; count = 0; }else { count++; if (count > 1000){ System.out.println(max); return; } } } } }
0.0分
1 人评分
C语言程序设计教程(第三版)课后习题8.9 (C++代码)浏览:844 |
C语言程序设计教程(第三版)课后习题11.12 (C语言代码)浏览:719 |
校门外的树 (C语言代码)浏览:1113 |
汽水瓶 (C语言代码)浏览:597 |
2006年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:464 |
用筛法求之N内的素数。 (C语言代码)浏览:664 |
1071题解浏览:484 |
2005年春浙江省计算机等级考试二级C 编程题(2) (C语言代码)浏览:567 |
交换Easy (C语言代码)浏览:759 |
理财计划 (C语言代码)浏览:465 |