科大讯飞笔试 AK
第一题:倒序输出即可
public class App { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); for (int i = n; i >= 1; i--) System.out.print(i+" "); } }
第二题:一次遍历:
public class App { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), k = sc.nextInt(); String s = sc.next(); if (s.length() == 1) { System.out.println(k); return; } int i = 1; for (i = 1; i < s.length(); i++) { int diff = s.charAt(i) - s.charAt(i - 1); k -= diff; if (k < 0) break; } System.out.println(i == s.length() ? k : -1); } }
第三题:记录重复长度
public class App { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int[] arr1 = new int[n], arr2 = new int[n]; Map<Integer, Integer> map = new HashMap<>(); for (int i = 0; i < n; i++) arr1[i] = sc.nextInt(); for (int i = 0; i < n; i++) { arr2[i] = sc.nextInt(); map.put(arr2[i], i); } long sum = (long) n * (n + 1); int cnt = 1; for (int i = 0; i < arr1.length; i++) { int idx = map.get(arr1[i]); if (idx < n - 1 && i < n - 1 && arr2[idx + 1] == arr1[i + 1]) { cnt++; } else { sum -= (long) (cnt + 1) * cnt / 2; cnt = 1; } } System.out.println(sum); } }#科大讯飞信息集散地##科大讯飞求职进展汇总#