topcoder

  1 #include <algorithm> 
  2 #include <cassert> 
  3 #include <cctype> 
  4 #include <cmath> 
  5 #include <complex> 
  6 #include <cstdio> 
  7 #include <cstdlib> 
  8 #include <cstring> 
  9 #include <ctime> 
 10 #include <deque> 
 11 #include <functional> 
 12 #include <iterator> 
 13 #include <iostream> 
 14 #include <list> 
 15 #include <map> 
 16 #include <numeric> 
 17 #include <queue> 
 18 #include <set> 
 19 #include <string> 
 20 #include <valarray> 
 21 #include <vector> 
 22 #include <iomanip>
 23 using namespace std;
 24 
 25 typedef long long ll; 
 26 typedef vector<int> vi; // vi(长度,值)
 27 typedef vector<string> vs ; 
 28 typedef vector<double> vd ; 
 29 typedef istringstream ISS;
 30 typedef ostringstream OSS;
 31 
 32 #define all(a) (a).begin(),(a).end() 
 33 #define mset(a,v) memset(a,v,sizeof(a)) 
 34 #define pb push_back   //a.pb( ) 
 35 #define min(a,b) ((a)<(b)?(a):(b)) 
 36 #define max(a,b) ((a)>(b)?(a):(b)) 
 37 #define sz size()
 38 #define rep(i,n) for(int i=0;i<(n);i++)
 39 #define swap(a,b) a^=b^=a^=b;
 40 
 41 
 42 
 43 class SwappingDigits
 44 {
 45 public:
 46     int minnum0(string num)
 47     {
 48         int i,min0=0;
 49         int len=num.length();
 50         for (i=len-1;i>0;i--)
 51         {
 52             if((num[i] < num[min0])&&(num[i] != '0'))
 53                 min0 = i;
 54         }
 55         return min0;
 56     }
 57     int minnump(string num,int j)
 58     {
 59         int i,minp=j;
 60         int len=num.length();
 61         for (i=len;i>j;i--)
 62         {
 63             if((num[i] < num[minp]))
 64                 minp = i;
 65         }
 66         return minp;
 67     }
 68     string minNumber(string num)
 69     {
 70         int i,j,mid;
 71         int len=num.length();
 72         mid = len/2;
 73 
 74         i = minnum0(num);
 75         if(i)
 76         {
 77             swap(num[i],num[0]);
 78             return num;
 79         }
 80 
 81         for(j=1;j<mid;j++)
 82         {
 83             i=minnump(num,j);
 84             if(i != j)
 85             {
 86                 swap(num[i],num[j]);
 87                 return num;
 88             }
 89         }
 90         
 91         return num;
 92     }
 93 };
 94 int main()
 95 {
 96     SwappingDigits sol;
 97     string s;
 98     while(true)
 99     {
100         cin>>s;
101         cout<<sol.minNumber(s)<<endl;
102     }
103 }

 

你可能感兴趣的:(topcoder)