# Untitled unknown
plain_text
2 months ago
4.6 kB
2
Indexable
Never
```/*Earning Biggest Prize Money 2
The winner in a quiz contest will be given an opportunity to earn bonus prize money. Among the given number cards, the winner can choose two number cards and exchange their positions as many times as the set number.

For example, let’s assume five number cards 3, 2, 8, 8, 8 are given as follows and the number of exchange is set to 2.

Before exchange>

3

2

8

8

8

For a first time, positions of 3 in the first card and 8 in the fourth card were switched and the order of the cards became 8, 2, 8, 3, 8.

8

2

8

3

8

Next, positions of 2 in the second card and 8 in the last card were switched and the order of the cards became 8, 8, 8, 3, 2.

8

8

8

3

2

After switch is made as many as the set number, prize money is calculated by the weight given to the positions of the cards. The prize money is 1 won at right end and it increases by ten times as it moves by one digit. When the order of the cards becomes 8,8,8,3,2 as shown in the example above, the winner will get the bonus prize money of 88832 won.

Note that switch must be made as many times as the set number, and the switch of the same positions switched can take place. In the following case, the number of exchange is set to 1, so switch must occur once to get 49 as the result.

9

4

Switch once->

4

9

In case of 94, when switch is made twice, the order goes back to the original number which is 94.

Now, compute the biggest amount of prize money the winner can get when number cards are switched as many times as the set number.

Input

A maximum of 50 test cases are given through standard input. The first line of the input has number of test cases T.(1 ≤ T ≤ 50). For each test case, the information on number cards and the number of exchange are given. The information on the number cards will be given in integer-type numbers, and the maximum number of digits is 6. Number of exchange is less than or equal to 10.

Output

Print “Case #C” in the first line for each test case with C being the case number. The next line print the biggest amount of prize money the winner can receive after exchange in the same line.

Input Example

Output Example

3    // number of testcase

123 1  // number cards, the number of exchange

2737 1 // number cards, the number of exchange

32888 2

Case #1

321

Case #2

7732

Case #3

88832
* */

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class CopyOfEarningBiggestPrizeMoney2 {
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 int kleft;
static String res;

static void swap(char[] str, int i, int j) {
char tmp = str[i];
str[i] = str[j];
str[j] = tmp;
}

static void sort(char[] inp, int k) {
if (k == 0)
return;

int n = inp.length;
for (int i = 0; i < n - 1; ++i)
for (int j = i + 1; j < n; ++j)
if (inp[j] > inp[i]) {
swap(inp, i, j);

String tmp = String.valueOf(inp);
if (tmp.compareTo(res) > 0) {
res = tmp;
kleft = k - 1;
}

sort(inp, k - 1);

swap(inp, j, i);
}
}

public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = Integer.parseInt(scanner.next());

out: for (int ti = 1; ti <= t; ++ti) {
char[] inp = scanner.next().toCharArray();
int ch = Integer.parseInt(scanner.next());
kleft = ch;
res = String.valueOf(inp);

sort(inp, ch);

if (kleft > 0) {
int n = inp.length;
int cnt[] = new int;
for (int i = 0; i < n; ++i)
++cnt[inp[i] - '0'];
for (int i = 0; i < 10; ++i)
if (cnt[i] > 1) {
System.out.printf("Case #%d\n%d\n", ti,
Integer.parseInt(res));
continue out;
}

if ((kleft & 1) == 0)
System.out.printf("Case #%d\n%d\n", ti,
Integer.parseInt(res));
else {
char[] chres = res.toCharArray();
swap(chres, chres.length - 1, chres.length - 2);
System.out.printf("Case #%d\n%d\n", ti,
Integer.parseInt(String.valueOf(chres)));
}
} else
System.out.printf("Case #%d\n%s\n", ti, res);
}

scanner.close();
}
}
```