ACM--欲張り--FZU-231-Min Number


FZUタイトル住所:http://acm.fzu.edu.cn/problem.php?pid=2111
                                           Problem 2111 Min Number
                                                                                         Acct:802    Submit:1587                                                                        Time Limit:1000 mSec    メモリLimit:32768 KB
Problem Description
Now you are given one non-negative integer n in 10-base notation、it will only contain digits('0'-'9')You are allowed to chose 2 integers i and j、such that:i!j,1≦iFor example、n=9012、we chose i=1、j=3、then we swap n[1]and n[3]、then we get 1092、which is smaler than the original n.
Now you are allowed to operate at most M times、so what is the smalest number you can get after the operation(s)?
Please note that in this proble、leading zeris not allowed!
Input
The first line of the input contains an integer T(T≦100)、indicating the number of test cases.
The n T cases、for any case、only 2 integers n and M(0≦n<10,1000,0≦M≦100)in a single line.
Output
For each test case,output the minimum number we can get afterのmore than M operation.
Sample Input
39012
Sample Output
9012129 
========================ツンデレの分割線======================================================================================
数nと交換回数mをあげます.m回交換した後の最小の数はいくらですか?先頭ゼロがあってはいけません.
そこで初めての交換を持ち出して単独で考えて、まず欲張りでゼロでない最小値を探して、最小値と第一位を交換して、一回ずつ交換します.
後ろも同じです.欲張りは後の桁の最小値を探して、前の数と比較して、前の数より小さいなら交換します.
             
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int main(){
       char num[2000];
       int i,j,n,m,len,index;
       cin>>n;
       while(n--){
         cin>>num;
         cin>>m;
         len=strlen(num);
         if(m!=0){//m 1        
              for(i=0,index=0;i<len;i++){
                   if(num[i]<num[index]&&num[i]!='0'){
                        index=i;
                   }
              }
              //       num[0]      
              if(num[0]>num[index]){
                    //  
                   swap(num[0],num[index]);
                   //    -1
                   m--;
              }
         }
         //       
         for(i=1;i<len&&m!=0;i++){
                //     
             for(j=i,index=j;j<len;j++){
                 if(num[j]<num[index]){
                    index=j;
                 }
             }
           //  
             if(num[i]>num[index]){
                swap(num[i],num[index]);
                m--;
             }
         }
         //         
         cout<<num<<endl;
       }
        return 0;
    }
ブログを参照してください:http://blog.csdn.net/whjkm/article/details/45921931