Write a solution to this in c++ Lookcook the geniosity easily finished the Canadian…
- User
Write a solution to this in c++ Lookcook the geniosity easily finished the Canadian Computing Competition this year and got a score of (n). He wants to reduce his score to avoid going to CCO and having to meet AQT. By hacking into the CCC database, he can take any digit of his score and move it to the right end in one operation. However, he can only make up to (K) operations before he is caught. What's the minimum score he can end up with?
Input Specification The first line contains (T), the number of test cases.
Then (T) test cases follow.
The first line of each case contains (n), an integer containing only digits from (1) to (9).
The second line contains (K), the number of digits you can move.
Output Specification The smallest number that can be obtained by moving at most (K) digits of (n) to the end.
Constraints (1 \le T \le 100,000)
(2 \le n \le 10^{100,000})
(0 \le K \le 100,000)
(n) will not contain zeroes, all digits will be from (1) to (9).
The product of (n) across all test cases will not exceed (10^{100,000}).
- Assistant
To solve this problem, we can first convert the integer (n) to a string. Then, we can iterate through the string and keep track of the number of operations performed ((op)). For each digit, we can check if we can move it to the right end ((K > 0)). If so, we can swap the digit with the next digit and decrement (K) and increment (op) by 1. We continue this process until (K) becomes 0 or we reach the end of the string.
- User
What did you mean by " K–" inside of the while loop
- Assistant
Apologies for the confusion. That was a typo in my previous response, it should be (K--) instead of (K-). The correct line of code should be
K--;.In C++,
--is the decrement operator, which subtracts 1 from the value of a variable. SoK--will decrement the value of (K) by 1. This line of code is used to reduce the number of digits that can be moved to the end ((K)) by 1 after each operation.