选择题一如既往地忘了
编程
1.求第n个数,规律是只含2,3,5,比如2,3,5,22,23,25,32,33,35,求第n个
我寻思这不是丑数改版吗?动态规划来做,l直接把leetcode丑数那道题改一下就可以了
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] dp = new int[n+1]; int p2 = 0, p3 = 0, p5 = 0; for (int i = 1; i <= n; i++) { int min = Math.min(dp[p2] * 10 + 2, Math.min(dp[p3] * 10 + 3, dp[p5] * 10 + 5)); if(min == dp[p2] * 10 + 2) p2++; if(min == dp[p3] * 10 + 3) p3++; if(min == dp[p5] * 10 + 5) p5++; dp[i] = min; } System.out.println(dp[n]); } }2.小球只能从左下,下,右下往下走,每个位置都有一个值,求从上到下的最大值
leetcode有差不多的好像,也是动态规划来做,这次的题是加了个左下方,不管,直接当成阶梯型就好。
public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] dp = new int[n][2 * n + 10]; for (int i = 0; i < n; i++) { for (int j = 0; j < 2 * i + 1; j++) { dp[i][j] = sc.nextInt(); } } for (int i = n - 2; i >= 0 ; i--) { for (int j = 0; j < 2 * i + 1; j++) { dp[i][j] += Math.max(dp[i+1][j], Math.max(dp[i+1][j+1], dp[i+1][j+2])); } } System.out.println(dp[0][0]); } }编程还是挺简单的,不到1个小时就做完全卷了。九点才交卷,现在就发是不是不太好?
全部评论
(1) 回帖