Search for the maximum number (3) and number (3)

Source: Internet
Author: User

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 ;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.