百度研发A卷 9.7 (100、100、25)
1.
import java.util.Scanner; /* 100 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int N = in.nextInt(); int K = in.nextInt(); int[][] dp = new int[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { dp[i][j] = in.nextInt(); } } for (int i = 0; i < N * K; i++) { for (int j = 0; j < N * K; j++) { int x = i / K; int y = j / K; System.out.print(dp[x][y] + " "); } System.out.println(); } } }2.
主要是考虑各种条件,使用string
可以自己测试
23011、1422 、 231103 这三个用例试试
import java.util.Scanner; /* 100 */ public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = Integer.parseInt(in.nextLine()); for (int i = 0; i < n; i++) { String str = in.nextLine(); int[] nums = new int[str.length()]; for (int k = 0; k < str.length(); k++) { nums[k] = str.charAt(k) - '0'; } boolean flag = false; for (int k = 0; k < nums.length; k++) { if (flag) { nums[k] = 3; } else if (nums[k] >= 1 && nums[k] <= 3) { continue; } else if (nums[k] > 3) { flag = true; nums[k] = 3; } else { if (!flag) { int temp = k - 1; while (temp != 0 && nums[temp] == 1) { temp--; } nums[temp]--; temp++; for (; temp < k; temp++) { nums[temp] = 3; } flag = true; } nums[k] = 3; } } int idx = 0; for (; idx < nums.length; idx++) { if (nums[idx] != 0) { break; } } StringBuilder sb = new StringBuilder(); for (; idx < nums.length; idx++) { sb.append(nums[idx]); } System.out.println(sb); } } }3. 只有25 等大佬题解
import java.util.HashMap; import java.util.Scanner; /* 25% */ public class Main { static int res = 0; public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); in.nextLine(); char[] chs = in.nextLine().toCharArray(); HashMap<Character, Integer> map = new HashMap<>(); if ( n < 32) { help(map, chs, 0, k); } System.out.println(res); } public static void help(HashMap<Character, Integer> map, char[] chs, int idx, int len) { if (map.size() == len) { res++; res %= (10e9 + 7); } if (map.size() > len || idx >= chs.length) { return; } for (int i = idx; i < chs.length; i++) { if (map.containsKey(chs[i])) { map.put(chs[i], map.getOrDefault(chs[i], 0) + 1); help(map, chs, i + 1, len); map.put(chs[i], map.get(chs[i]) - 1); // int l = map.get(chs[i]); // if (l == 1) { // map.remove(chs[i]); // } // else { // map.put(chs[i], l - 1); // } } else if (map.size() < len){ map.put(chs[i], 1); help(map, chs, i + 1, len); map.remove(chs[i]); } } } }