8.28华为笔试
第一题:
采用暴力解:
for (int i = 1; i < (p/3+1); i++) { for (int j = i; j < (p/2+1); j++) { if ((i*i + j*j) == (p-i-j)*(p-i-j)) { count++; } } }第二题:
左神算法课上求矩阵中岛屿数量问题(也是leetcode200),稍微改编一下就行了:
这道题我没过,当时在helper()函数中,条件没有判断正确,可急skr我了
我这还没加多次输入.............可真急skr我了.......
第三题没看.........要gg了
#华为##笔试题目#import java.util.HashMap; import java.util.Map; import java.util.Scanner; public class Test4 { public static int count = 0; public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] input = new int[6]; int[][] target = {{1,2,3,4,5}, {11,12,13,14,15}, {21,22,23,24,25}, {31,32,33,34,35}, {41,42,43,44,45}}; Map<Integer, Integer> map = new HashMap<Integer, Integer>(); for (int i = 0; i < 6; i++) { input[i] = sc.nextInt(); map.put(input[i], 1); } int m = 0; int n = 0; boolean isHave = false; for ( m = 0; m < target.length; m++) { for (n = 0; n < target[0].length; n++) { if (map.containsKey(target[m][n])) { helper(target,m,n,map); if (count == 6) { System.out.println(1); }else { System.out.println(0); } isHave = true; break; } } if (isHave) { break; } } } private static void helper(int[][] target,int i,int j,Map<Integer, Integer> map) { if (i<0 || i>=target.length || j<0 || j>= target[0].length || !map.containsKey(target[i][j])) { return; } if (map.containsKey(target[i][j])) { count++; target[i][j] = -1; } helper(target, i-1, j, map); helper(target, i, j+1, map); helper(target, i+1, j, map); helper(target, i, j-1, map); } }
第三题没看.........要gg了