1783. Large is Better
Constraints
Time Limit: 1 secs, Memory Limit: 64 MB
Description
Xiaosmall got a number from TDJ, which she considered to be too small. She wants to make the number as large as possible by swapping adjacent digits of the number. She can swap any adjacent digits for any number of times, except the digit 0. She's not allowed to swap any digit with digit 0.Given the number from TDJ, can you tell Xiaosmall the largest number she can achieve?
Input
The first line of the input is a positive integer L indicating the number of cases. Next L lines each containing a positive integer. No integer consists of more than 100 digits.
Output
For each case, output one integer in a line, which is the largest number Xiaosmall can achieve by swapping not-zero digits.
Sample Input
3 1 123 1012400198
Sample Output
1 321 1042100981
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main () {
int T;
cin>>T;
while (T--) {
string a; //用来记录大string
cin>>a;
int b[100] = {0}; //用来记录小string
int j = 0; //标记小string的下标
for (int i = 0; i < a.length(); i++) {
if (a[i] != '0') { //当不是0的时候 记录小string
b[j] = a[i] - '0';
j++;
} else if (a[i] == '0') { //是0的时候 如果前面有小string就输出前面的小string 然后输出0
if (j != 0) {
sort(b, b + j ); //对小string的数字排序
for (int k = j - 1; k >= 0; k--)
cout<<b[k];
j = 0; //小string下标设为0 说明小string已全部输出
}
cout<<'0';
}
}
if (j != 0) { //最后若不是0 即还有小string没输出
sort(b, b + j );
if (j == 1)
cout<<b[0];
else {
for (int k = j - 1; k >= 0; k--)
cout<<b[k];
j = 0;
}
}
cout<<endl;
}
//system("pause");
return 0;
}
不知道为什么有种什么都要靠自己但是自己好没用的挫败感……= ^ =