科大讯飞,8.15 研发类笔试(二) 算法代码。
第一次全AC
1、定义一个n*m的数字矩阵,找出不同行不同列的两个数的乘积的最大值。
暴力,没想到能AC
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); long[][] arr = new long[n][m]; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { arr[i][j] = scanner.nextLong(); } } long max = 0; for (int i = 0; i < n; i++) { for (int j = 0; j < m; j++) { long one = arr[i][j]; for (int k = 0; k < n; k++) { for (int z = 0; z < m; z++) { if (k != i && z != j) { long two = arr[k][z]; max = Math.max(one * two, max); } } } } } System.out.println(max); }2、插入排序,没什么好说的
3、求32位整数N的二进制1的个数。
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); long target = scanner.nextLong(); int count = 0; while (target>0){ target&=(target-1); count++; } System.out.println(count); }
4、将一个字符串长度为m的字符串左移n位
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String m = scanner.nextLine(); int n = scanner.nextInt()% m.length(); System.out.println(m.substring(n,m.length()) + m.substring(0,n)); }