第一题:暴力转换为String替换 2-3-5
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int temp = 0;
for (Integer i = 2; i < Integer.MAX_VALUE; i ++ ) {
String tmp = i.toString();
tmp = tmp.replace("2","").replace("3", "").replace("5", "");
if (tmp.length() == 0) {
temp++;
}
if (temp == n) {
System.out.println(i);
break;
}
}
}
}
第二题DP
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[][] dp = new int[n+1][2 * n+1];
for (int i = 1; i < n+1; i++) {
for (int j = 2; j < 2 * i +1; j++) {
dp[i][j]=input.nextInt();
}
}
int max=Integer.MIN_VALUE;
for (int i = 1; i < n+1; i++) {
for (int j = 2; j < 2 * n+1 ; j++) {
dp[i][j] = Math.max(Math.max(dp[i-1][j], dp[i-1][j-2]), dp[i-1][j-1]) + dp[i][j];
if (dp[i][j]>max){
max=dp[i][j];
}
}
}
System.out.println(max);
}
}
全部评论
(2) 回帖