拼多多笔试2022年3月6日
我小菜鸡,乱写Ac了3道,第三道不会,乱写的,过了15%。
第一道:
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String str = scanner.next(); int n=str.length(); StringBuilder sb = new StringBuilder(); int i=0; while (i < n) { int count =1; char c = str.charAt(i); i++; while(i<n&&str.charAt(i)==c){ count++; i++; } sb.append(count); sb.append(c); } System.out.println(sb.toString()); }第二道
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); int[] res = new int[T]; for(int i=0;i<T;i++ ) { int K = scanner.nextInt(); int L = scanner.nextInt(); String str = scanner.next(); Queue<String> queue = new LinkedList<>(); queue.add(str); int count=0,special=0; while (K>0&&!queue.isEmpty()){ for(int j=queue.size();j>0;j--) { String tmp = queue.poll(); if (!check(tmp)) { queue.add(reverse(tmp) + tmp); queue.add(tmp + reverse(tmp)); }else{ special++; } } K--; } count = special + queue.size(); res[i] = count; } for (int re : res) { System.out.println(re); } } private static String reverse(String s) { StringBuilder stringBuilder = new StringBuilder(s); return stringBuilder.reverse().toString(); } private static boolean check(String string) { int n=string.length(),left=0,right=0; if(n%2==1){ left=n/2; right=left; }else{ left=n/2-1; right=n/2; } while(left>=0&&right<n){ if(string.charAt(left)==string.charAt(right)){ left--; right++; }else{ return false; } } return true; } }第四道
import java.util.Scanner; public class Demo4 { static int sum = 100; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int T = scanner.nextInt(); int[] res = new int[T]; for (int i = 0; i < T; i++) { int[] arr1 = new int[6]; int[] arr2 = new int[6]; for (int i1 = 0; i1 < 6; i1++) { arr1[i1 ] = scanner.nextInt(); } for (int i1 = 0; i1 < 6; i1++) { arr2[i1 ] = scanner.nextInt(); } dfs(arr1, arr2,0); if (sum == 100) { res[i] = -1; } else { res[i] = sum; sum = 100; } } for (int i = 0; i < T; i++) { System.out.println(res[i]); } } private static void dfs(int[] arr1, int[] arr2, int count) { if (eq(arr1, arr2)) { sum = Math.min(count, sum); return ; } if(count>4){ return; } forward(arr1); dfs(arr1, arr2, count + 1); backward(arr1); backward(arr1); dfs(arr1, arr2, count + 1); forward(arr1); left(arr1); dfs(arr1, arr2, count + 1); right(arr1); right(arr1); dfs(arr1, arr2, count + 1); left(arr1); } private static boolean eq(int[] arr1, int[] arr2) { for(int i=0;i<6;i++) { if (arr1[i] != arr2[i])return false; } return true; } //sxzyqh. private static void forward(int[]arr) { int tmp = arr[4]; arr[4] = arr[0]; arr[0] = arr[5]; arr[5]=arr[1]; arr[1] = tmp; } private static void backward(int[]arr) { int tmp = arr[5]; arr[5] = arr[0]; arr[0] = arr[4]; arr[4]=arr[1]; arr[1] = tmp; } private static void left(int[]arr) { int tmp = arr[2]; arr[2] = arr[0]; arr[0] = arr[3]; arr[3]=arr[1]; arr[1] = tmp; } private static void right(int[]arr) { int tmp = arr[3]; arr[3] = arr[0]; arr[0] = arr[2]; arr[2]=arr[1]; arr[1] = tmp; } }
第三道怎么写啊,求告知