Search for the maximum number (3) and number (3)
Max search (3) Time Limit: 1000 MS | memory limit: 65535 KB difficulty: 2
-
Description
-
Given an integer N, you can move the numbers on two adjacent digits at most K times to get a new integer.
Evaluate the maximum value of the new integer.
-
Input
-
Multiple groups of test data.
Each group of test data occupies one row, with two numbers of N and K (1 ≤ limit N ≤ limit 10 ^ 18; 0 ≤ limit K ≤ limit 100 ).
-
Output
-
The output of each group of test data occupies one row. After the output is moved, the maximum value of the new integer is obtained.
-
Sample Input
-
1990 1100 09090000078001234 6
-
Sample output
-
91901009907000008001234
-
-
Ideas:
-
At first, I thought that I would move one digit forward than the previous one. The result was WA. Later I Thought It Was moving within the range of the number of moving steps. I was very happy to submit it, the result is WA again. I used to make a careless mistake. It was originally moved one by one, but I forgot it all at once. The result was a direct subscript swap element.
-
My code:
-
-
# Include <iostream> # include <algorithm> using namespace std; int main () {int k, I, j, t; char a [25], B; while (cin> a> k) {for (I = 0; a [I]! = '\ 0' & k> 0; ++ I) // array length {B = a [I]; for (j = I + 1; j <= I + k & a [j]! = '\ 0'; ++ j) // obtain the maximum number in the k range {if (B-'0' <a [j]-'0 ') {B = a [j]; t = j ;}} if (B! = A [I]) {k-= t-I; for (j = t; j> I; -- j) {swap (a [j], a [J-1]) ;}}cout <a <endl;} return 0 ;}