4.12贝壳找房笔试
第三题为什么case 0?求大佬指正!思路是有限的次数下使得整个数字升序排列!
import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); sc.nextLine(); String[][] datas = new String[t][2]; for (int i = 0; i < t; i++) { datas[i] = sc.nextLine().split(" "); } for (int k = 0; k < t; k++) { String[] cur = datas[k]; char[] arr = cur[0].toCharArray(); int times = Integer.parseInt(cur[1]); while (times-- > 0){ int i = 1; for (i = 1; i < arr.length; i++) { if (arr[i] < arr[i-1]){ swap(arr, i, i-1); break; } } if (i == arr.length){ break; } } int f = 0; for (f = 0; f < arr.length; f++) { if (arr[f] != '0'){ break; } } arr = Arrays.copyOfRange(arr,f,arr.length); String res = new String(arr); System.out.println(res); } } public static void swap(char[] arr, int i, int j){ char temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } }第一题求和比较,第二题从后往前回溯,第四题感觉是并查集+回溯(没时间做,都耗在case0了)