好简单啊,第一次笔试全AC,纪念一下。
public class test1 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[]{2,3,5}; Queue<StringBuilder> queue = new LinkedList<>(); queue.add(new StringBuilder("0")); while(!queue.isEmpty() && n > 0){ StringBuilder temp = queue.poll(); for(int i = 0 ; i < a.length; i++){ temp.append(a[i]); queue.add(new StringBuilder(temp)); temp.setLength(temp.length()-1); n--; if(n == 0){ break; } } } while(queue.size() != 1){ queue.poll(); } int ans = Integer.parseInt(queue.poll().substring(1)); System.out.println(ans); } }
public class test2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[][] grid = new int[n][2*n-1]; String[] temp= new String[n]; sc.nextLine(); for(int i = 0; i< n; i++) { temp[i] = sc.nextLine(); String[] str = temp[i].trim().split(" "); for(int j = 0; j < str.length; j++){ grid[i][j] = Integer.parseInt(str[j]); } } for(int i = n-2; i >= 0; i--){ for(int j = 0; j < 2*(i+1)-1; j++){ grid[i][j] += Math.max(grid[i+1][j], Math.max(grid[i+1][j+1], grid[i+1][j+2])); } } System.out.println(grid[0][0]); } }
全部评论
(3) 回帖