标签:+= oid ann div res 忘记 ++ inpu hashset
第一题
import java.util.HashSet; import java.util.Scanner; import java.util.Set; public class Main1 { public static void main(String[] args) { Scanner input = new Scanner(System.in); String str = input.nextLine(); String[] arr = str.split(","); Set<String> set = new HashSet<>(); for (int i = 0; i < arr.length; i++) { set.add(arr[i]); } System.out.println(set.size()); input.close(); } }
第二题
import java.util.Scanner; public class Main2 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int n = input.nextInt(); int[] arr = new int[n]; for (int i = 0; i < arr.length; i++) { arr[i] = input.nextInt(); } int[] left = new int[n]; for (int i = 1; i < arr.length; i++) { if (arr[i] > arr[i - 1]) { left[i] = left[i - 1] + 1; } if (arr[i]==arr[i-1]){ left[i]=left[i-1]; } } int[] right = new int[n]; for (int i = arr.length - 2; i > 0; i--) { if (arr[i] > arr[i + 1]) { right[i] = right[i + 1] + 1; } if (arr[i] == arr[i + 1]) { right[i] = right[i + 1]; } } int[] result = new int[n]; for (int i = 0; i < n; i++) { if (left[i] >= right[i]) { result[i] = left[i]; } else { result[i] = right[i]; } } int sum = n; for (int i = 0; i < n; i++) { sum += result[i]; } System.out.println(sum); input.close(); } }
第三题
import java.util.Scanner; public class Main3 { public static void main(String[] args) { Scanner input = new Scanner(System.in); int a = input.nextInt(); int b = input.nextInt(); int[][] arr = new int[a][b]; int[][] result = new int[a][b]; for (int i = 0; i < a; i++) { for (int j = 0; j < b; j++) { arr[i][j] = input.nextInt(); } } result[0][0] = arr[0][0]; for (int i = 1; i < a; i++) { result[i][0] = result[i - 1][0] + arr[i][0]; } for (int i = 1; i < b; i++) { result[0][i] = result[0][i - 1] + arr[0][i]; } for (int i = 1; i < a; i++) { for (int j = 1; j < b; j++) { result[i][j] = Math.min(result[i][j - 1], result[i - 1][j]) + arr[i][j]; } } System.out.println(result[a - 1][b - 1]); input.close(); } }
标签:+= oid ann div res 忘记 ++ inpu hashset
原文地址:https://www.cnblogs.com/GG-Bond/p/11571393.html