序列在Java中其实就是集合,这道题其实是考察我们对集合的理解,大多数选项都是集合的自带的方法,但是那个在i和i+1之间插入的,我一下子找不到对应方法只能手写了
import java.util.ArrayList; import java.util.Scanner; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub ArrayList<Integer> list = new ArrayList<>(); Scanner scanner = new Scanner(System.in); int q = scanner.nextInt(); for (int i = 0; i < q; i++) { int n = scanner.nextInt(); if (n == 1) { int x = scanner.nextInt(); list.add(x); } else if (n == 2) { list.remove(list.size() - 1); } else if (n == 3) { int j = scanner.nextInt(); System.out.println(list.get(j)); } else if (n == 4) { int j = scanner.nextInt(); int x = scanner.nextInt(); int a[] = new int[list.size() + 1]; a[j + 1] = x; ArrayList<Integer> list1 = new ArrayList<>(); for (int k = 0; k < a.length; k++) { if (k == j + 1) { list1.add(a[k]); continue; } if (k > j + 1) { a[k] = list.get(k - 1); } else { a[k] = list.get(k); } list1.add(a[k]); } list = new ArrayList<>(list1); } else if (n == 5) { list.sort((s1, s2)->s1 - s2); } else if (n == 6) { list.sort((s1, s2)->s2 - s1); } else if (n == 7) { System.out.println(list.size()); } else if (n == 8) { for (Integer j : list) { System.out.print(j + " "); } System.out.println(); } } } }