米哈游0319笔试
第一次写//
发一下昨晚做题的思路 时间不够用啊 第三题有运行错误 没调试出来
只有第一题并查集全a了
package travers; import java.util.Scanner; public class mihay { static int n; static int m; static char[][] board, board2; public static void main(String[] args) { Scanner sc = new Scanner(System.in); n = sc.nextInt(); m = sc.nextInt(); String[] s = new String[n]; board = new char[n][m]; for (int i = 0; i < n; i++){ s[i] = sc.next(); board[i] = s[i].toCharArray(); } int res = 0; int res1 = 0; board2 = new char[n][m]; for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ board2[i][j] = board[i][j] == 'G' ? 'B' : board[i][j]; } } for (int i = 0; i < n; i++){ for (int j = 0; j < m; j++){ if (board[i][j] != 'q'){ res++; dfs(board, i, j, board[i][j]); } if (board2[i][j] != 'q'){ res1++; dfs(board2, i, j, board2[i][j]); } } } System.out.println(res - res1); } public static void dfs(char[][] board, int i, int j, char temp){ if (i < 0 || j < 0 || i > n - 1 || j > m - 1 || board[i][j] != temp ||board[i][j] == 'q') { return; } char tmp = board[i][j]; board[i][j] = 'q'; dfs(board, i-1, j, tmp); dfs(board, i+1, j, tmp); dfs(board, i, j-1, tmp); dfs(board, i, j+1, tmp); } }
第二题 哈希映射0.5(不知道为啥) 、
package travers; import java.util.HashMap; import java.util.Scanner; public class mihay2 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = 0; i < n; i++){ String s = sc.next(); String t = sc.next(); String s1 = s.length() > t.length() ? s : t; String t1 = s.length() > t.length() ? t : s; HashMap<Character, Integer> map = new HashMap<>(); for (int j = 0; j < s1.length(); j++){ char ch = s1.charAt(j); map.put(ch, map.getOrDefault(ch, 0) + 1); } boolean flag = true; for (int j = 0; j < t1.length(); j++){ char ch2 = t1.charAt(j); if (!map.containsKey(ch2)){ flag = false; break; } map.put(ch2, map.get(ch2) - 1); } if (flag){ for (Character ch : map.keySet()){ if (ch == 'm' || ch == 'h' || ch == 'y' || map.get(ch) == 0) { continue; } flag = false; break; } } if (map.get('m') != map.get('h') || map.get('m') != map.get('y') || !flag) System.out.println("No"); else System.out.println("Yes"); } } }
第三题感觉是个子集dfs的思路 但是呢找不到错哪
package travers; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class mihay3 { static List<List<Integer>> res = new ArrayList<>(); static List<Integer> tmp = new ArrayList<>(); public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] a = new int[n]; for (int i = 0; i < n; i++){ a[i] = sc.nextInt(); } dfs(a, 0); System.out.println(res.size()); } public static void dfs(int[] nums, int startInd){ if (tmp.size() >= 2){ if (isValid(tmp)){ res.add(new ArrayList<>(tmp)); } else return; } for (int i = startInd; i < nums.length; i++){ tmp.add(nums[i]); dfs(nums, i + 1); tmp.remove(tmp.size() - 1); } } public static boolean isValid(List<Integer> nums){ if (nums.size() == 2){ int a = nums.remove(nums.size()-1); int b = nums.remove(nums.size()-1); return a % b == 0 ? true : false; } int a = nums.remove(nums.size()-1); int b = nums.remove(nums.size()-1); int temp = a / b; while (!tmp.isEmpty()){ a = b; b = nums.remove(nums.size()-1); if (a / b != temp) return false; } return true; } }
第二题第三题有没有大神指导一下
#暑假实习##米哈游##java##笔试#