2022天翼云 第一题50% 其他ac
天翼云是真懒啊 2021年的编程题拿来直接用????
第一题:
import java.util.*; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); List<int[]> input = new ArrayList<>(); s.nextLine(); while(n > 0){ String[] s1 = s.nextLine().split(","); int[] cur = new int[2]; cur[0] = Integer.parseInt(s1[0]); cur[1] = Integer.parseInt(s1[1]); input.add(cur); n--; } int length = sumLength(input); System.out.println(length); } public static int sumLength(List<int[]> input){ if(input.isEmpty()){ return 0; } int len = input.size(); if(len == 1){ return input.get(0)[1] - input.get(0)[0]; } input.sort(new Comparator<int[]>() { @Override public int compare(int[] o1, int[] o2) { return o1[1] - o2[0]; } }); for (int i = 1; i < len; i++) { if(input.get(i)[0] < input.get(i-1)[1]){ input.get(i)[0] = input.get(i-1)[1]; input.get(i-1)[1] = input.get(i)[0]; } } int res = 0; for (int i = 0; i < len; i++) { if(input.get(i)[0] >= input.get(i)[1]){ continue; } res += input.get(i)[1] - input.get(i)[0]; } return res; } }
第二题:
public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] input = new int[n]; String s1 = s.nextLine(); String s2 = s.nextLine(); String[] split = s2.split(","); for (int i = 0; i < split.length; i++) { input[i] = Integer.parseInt(split[i]); } int[] res = reOrder(input); for (int i = 0; i < res.length - 1; i++) { System.out.print(res[i] + ","); } System.out.println(res[res.length - 1]); } private static int[] reOrder(int[] input) { int len = input.length; // 奇数 List<Integer> oddList = new ArrayList<>(); List<Integer> evenList = new ArrayList<>(); for (int i = 0; i < len; i++) { if(input[i] % 2 == 0){ evenList.add(input[i]); }else{ oddList.add(input[i]); } } int[] res = new int[len]; for (int i = 0; i < len; i++) { if(!evenList.isEmpty()){ res[i] = evenList.remove(0); }else{ res[i] = oddList.remove(0); } } return res; } }
第三题:....第三题怎么放错了,,,跟第二题一样了
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner s = new Scanner(System.in); int n = s.nextInt(); int[] input = new int[n]; String s1 = s.nextLine(); String s2 = s.nextLine(); String[] split = s2.split(","); for (int i = 0; i < split.length; i++) { input[i] = Integer.parseInt(split[i]); } int[] res = reOrder(input); for (int i = 0; i < res.length - 1; i++) { System.out.print(res[i] + ","); } System.out.println(res[res.length - 1]); } private static int[] reOrder(int[] input) { int len = input.length; // 奇数 List<Integer> oddList = new ArrayList<>(); List<Integer> evenList = new ArrayList<>(); for (int i = 0; i < len; i++) { if(input[i] % 2 == 0){ evenList.add(input[i]); }else{ oddList.add(input[i]); } } int[] res = new int[len]; for (int i = 0; i < len; i++) { if(!evenList.isEmpty()){ res[i] = evenList.remove(0); }else{ res[i] = oddList.remove(0); } } return res; } }