Untitled
plain_text
2 months ago
1.9 kB
5
Indexable
Never
import java.io.FileInputStream; import java.io.FileNotFoundException; import java.util.Scanner; public class EarningBiggestPrizeMoney2 { static { try { System.setIn(new FileInputStream("src/inp.txt")); // System.setOut(new PrintStream("src/out.txt")); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } } static Scanner scanner = new Scanner(System.in); static String res; static int kleft; static void swap(char[] s, int a, int b) { char tmp = s[a]; s[a] = s[b]; s[b] = tmp; } static void Try(char[] str, int k) { if (k == 0) return; int n = str.length; for (int i = 0; i < n - 1; ++i) for (int j = i + 1; j < n; ++j) if (str[j] > str[i]) { swap(str, i, j); String tmp = String.valueOf(str); if (tmp.compareTo(res) > 0) { res = tmp; kleft = k - 1; } Try(str, k - 1); swap(str, i, j); } } static void exec(int t) { res = scanner.next(); int nch = Integer.parseInt(scanner.next()); kleft = nch; Try(res.toCharArray(), nch); if (kleft > 0) { int[] chk = new int[10]; boolean chk2 = false; for (int i = 0; i < res.length(); ++i) chk[res.charAt(i) - '0']++; for (int i = 0; i < 10; ++i) if (chk[i] > 1) { chk2 = true; break; } if ((kleft & 1) == 0 || chk2) System.out.printf("Case #%d\n%d\n", t, Integer.parseInt(res)); else { char[] res2 = res.toCharArray(); swap(res2, res2.length - 1, res2.length - 2); System.out.printf("Case #%d\n%d\n", t, Integer.parseInt(String.valueOf(res2))); } } else System.out.printf("Case #%d\n%d\n", t, Integer.parseInt(res)); } public static void main(String[] args) { int t = Integer.parseInt(scanner.next()); for (int i = 1; i <= t; ++i) exec(i); scanner.close(); } }