一共三题,总共2h30min,总体看题目特别长,先是大概浏览一遍,想把有思路的做了,结果题目都很长🤐,看的人头疼
硬着头皮上
第一题:判断麻将是否能胡(对于麻将规则一无所知的人🤣),好在过了AC
import java.util.*; public class Test4 { private static Map<String, String[]> map = new HashMap<>(); public static void main(String[] args) { Scanner in = new Scanner(System.in); int T = Integer.valueOf(in.nextLine()); helper(map); for (int i = 0; i < T; i++) { String[] str = (in.nextLine()).split(" "); if (helper2(str, i)) { System.out.println("YES"); } else { System.out.println("NO"); } } } private static boolean helper2(String[] str, int i) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(); StringBuilder sb3 = new StringBuilder(); Set<String> set = new HashSet<>(); StringBuilder goal = new StringBuilder("abc"); for (int j = 0; j < str.length; j++) { String s = str[j]; if (set.contains(s)) { return false; } set.add(s); if (s.charAt(0) == '1' || s.charAt(0) == '4' || s.charAt(0) == '7') { sb1.append(s.charAt(0)); if (goal.charAt(0) != 'a' && goal.charAt(0) != s.charAt(1)) { return false; } goal.setCharAt(0, s.charAt(1)); } if (s.charAt(0) == '2' || s.charAt(0) == '5' || s.charAt(0) == '8') { sb2.append(s.charAt(0)); if (goal.charAt(1) != 'b' && goal.charAt(1) != s.charAt(1)) { return false; } goal.setCharAt(1, s.charAt(1)); } if (s.charAt(0) == '3' || s.charAt(0) == '6' || s.charAt(0) == '9') { sb3.append(s.charAt(0)); if (goal.charAt(2) != 'c' && goal.charAt(2) != s.charAt(1)) { return false; } goal.setCharAt(2, s.charAt(1)); } } if (goal.charAt(0) == 'a' || goal.charAt(1) == 'b' || goal.charAt(2) == 'c') { return false; } sb1.append(goal.charAt(0)); sb2.append(goal.charAt(1)); sb3.append(goal.charAt(2)); char[] chars1 = sb1.toString().toCharArray(); char[] chars2 = sb2.toString().toCharArray(); char[] chars3 = sb3.toString().toCharArray(); Arrays.sort(chars1); Arrays.sort(chars2); Arrays.sort(chars3); String s1 = new String(chars1); String s2 = new String(chars2); String s3 = new String(chars3); return helper3(goal.toString(), s1, s2, s3); } private static boolean helper3(String goal, String s1, String s2, String s3) { String[] str = map.get(goal); System.out.println(Arrays.toString(str)); for (int i = 0; i < s1.length(); i++) { if (str[0].indexOf(s1.charAt(i)) == -1) { return false; } } for (int i = 0; i < s2.length(); i++) { if (str[1].indexOf(s2.charAt(i)) == -1) { return false; } } for (int i = 0; i < s3.length(); i++) { if (str[2].indexOf(s3.charAt(i)) == -1) { return false; } } return true; } private static void helper(Map<String, String[]> map) { String[] s1 = {"147W", "258B", "369T"}; String[] s2 = {"147W", "258T", "369B"}; String[] s3 = {"147T", "258W", "369B"}; String[] s4 = {"147T", "258B", "369W"}; String[] s5 = {"147B", "258T", "369W"}; String[] s6 = {"147B", "258W", "369T"}; map.put("WBT", s1); map.put("WTB", s2); map.put("TWB", s3); map.put("TBW", s4); map.put("BTW", s5); map.put("BWT", s6); } }
第二题:十字斩游戏,AC
import java.util.*; public class Test6 { public static void main(String[] args) { Scanner in = new Scanner(System.in); while (in.hasNextLine()) { int n = Integer.valueOf(in.nextLine()); int[][] arr = new int[n][n]; for (int i = 0; i < n; i++) { String[] str = (in.nextLine()).split(" "); for (int j = 0; j < str.length; j++) { arr[i][j] = Integer.valueOf(str[j]); } } List<List<Integer>> res = new ArrayList<>(); while (true) { int[][] books = new int[arr.length][arr.length]; helper(res, books, arr, arr.length); arr = helper2(books, arr); if (arr == null) { break; } } for (int i = 0; i < res.size(); i++) { for (int j = 0; j < res.get(i).size(); j++) { System.out.print(res.get(i).get(j)); if (j != res.get(i).size() - 1) { System.out.print(" "); } } System.out.println(); } } } private static void helper(List<List<Integer>> res, int[][] books, int[][] arr, int len) { int[] hen = new int[len]; int[] zon = new int[len]; for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { hen[i] += arr[i][j]; zon[j] += arr[i][j]; } } int max = 0; int x = 0; int y = 0; for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { int count = hen[i]; count += zon[j]; count -= arr[i][j]; if (count > max) { max = count; y = i; x = j; } } } for (int i = 0; i < len; i++) { for (int j = 0; j < len; j++) { if (i == y || x == j) { books[i][j] = -1; } } } List<Integer> list = new ArrayList<>(); list.add(y + 1); list.add(x + 1); res.add(list); } private static int[][] helper2(int[][] books, int[][] arr) { List<List<Integer>> res = new ArrayList<>(); for (int i = 0; i < arr.length; i++) { List<Integer> list = new ArrayList<>(); for (int j = 0; j < arr.length; j++) { if (books[i][j] == 0) { list.add(arr[i][j]); } } if (list.size() == 0) { continue; } res.add(list); } if (res.size() == 0) { return null; } int[][] newArr = new int[res.size()][]; for (int i = 0; i < res.size(); i++) { List<Integer> list = res.get(i); int[] tmp = new int[list.size()]; for (int j = 0; j < list.size(); j++) { tmp[j] = list.get(j); } newArr[i] = tmp; } return newArr; } }
第三题:找出自身消耗时间最长的值作代号,这类游戏玩过,情景较熟悉,但是最后没时间了,应该通过递归能写出来
好不容易能做出两题,前面笔试都很差,希望这次能有个面试机会
全部评论
(1) 回帖