科大讯飞 8.29 AK
第一题
import java.util.Scanner; public class Keda1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String line = input.nextLine(); String[] nums = line.split(","); int n = Integer.valueOf(nums[0]); int m = Integer.valueOf(nums[1]); int[][] pan = new int[n][m]; for (int i = 0; i < n; i++) { String[] stmp = input.nextLine().split(" "); for (int j = 0; j < m; j++) { pan[i][j] = Integer.valueOf(stmp[j]); } } int[][] ans = pan; for (int i = 1; i < m; i++) { ans[0][i] = ans[0][i] + ans[0][i - 1]; } for (int i = 1; i < n; i++) { ans[i][0] = ans[i][0] + ans[i - 1][0]; } for (int i = 1; i < n; i++) { for (int j = 1; j < m; j++) { ans[i][j] = Math.max(ans[i - 1][j], ans[i][j - 1]) + ans[i][j]; } } System.out.println(ans[n - 1][m - 1]); } }
第二题
import java.util.Scanner; public class Keda2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] nums = new int[n]; String s = input.next(); String[] strings = s.split(","); for (int i = 0; i < n; i++) { nums[i] = Integer.valueOf(strings[i]); } sort(nums, n); StringBuilder ans = new StringBuilder(); for (int i = 0; i < n; i++) { if (i == n - 1) { ans.append(nums[i] + ""); } else { ans.append(nums[i] + ","); } } System.out.println(ans.toString()); } private static void sort(int[] nums, int n) { for (int i = 0; i < n; i++) { int minNum = nums[i]; int index = i; for (int j = i + 1; j < n; j++) { if (nums[j] < minNum) { index = j; minNum = nums[j]; } } if (index != i) { int tmp = nums[i]; nums[i] = nums[index]; nums[index] = tmp; } } } }
第三题
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Keda3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String line = input.nextLine(); List<Character> res = new ArrayList<Character>(); char[] chars = line.toCharArray(); int n = chars.length; for (int i = 0; i < n; i++) { int j = i; boolean flag = true; while (j < n && chars[j] == '_') { j++; if (i != 0 && flag) { res.add('_'); flag = false; } } if (j < n && chars[j] != '_') { res.add(chars[j]); i = j; } } int size = res.size(); for (int i = size - 1; i >= 0; i--) { Character character = res.get(i); if (character != '_') { break; } else if (character == '_') { res.remove(i); } } StringBuilder ans = new StringBuilder(); for (Character re : res) { ans.append(re); } System.out.println(ans.toString()); } }
第四题
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Keda4 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); if (isZhiShu(n)) { System.out.println(n); } List<Integer> ans = new ArrayList<Integer>(); zheng(n, ans, 2); int size = ans.size(); StringBuilder stringBuilder = new StringBuilder(); for (int i = 0; i < size; i++) { if (i == size - 1) { stringBuilder.append(ans.get(i)); } else { stringBuilder.append(ans.get(i) + "*"); } } System.out.println(stringBuilder.toString()); } private static void zheng(int n, List<Integer> ans, int x) { if (isZhiShu(n)) { ans.add(n); return; } if (isZhiShu(x)) { if (n % x == 0) { ans.add(x); zheng(n / x, ans, x); } else { zheng(n, ans, x + 1); } } else { zheng(n, ans, x + 1); } } private static boolean isZhiShu(int n) { for (int i = 2; i <= Math.sqrt(n); i++) { if (n % i == 0) { return false; } } return true; } }