1783. Large is Better 用数组和string 对数字排序

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

不知道为什么有种什么都要靠自己但是自己好没用的挫败感……= ^ =

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值