hrbust 1387 Shuffle the Digits【贪心】

Shuffle the Digits
Time Limit: 1000 MSMemory Limit: 65536 K
Total Submit: 165(68 users)Total Accepted: 75(66 users)Rating: Special Judge: No
Description

Leyni gets an integer n.

He wants to shuffle the digits in the integer n to produce a smallest possible integer without leading zeros. (None of the digits in the integer can be removed or replaced.)

Please help him solve this problem!

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. Then T test cases follow.

For each test case:

Line 1. This line contains an integer n (0 ≤ n ≤ 109).

Output

For each test case:

Line 1. Output the new integer that Leyni wants.

Sample Input

1

4321

Sample Output

1234

Source
哈理工2012春季校赛 - 现场赛
Author
齐达拉图@HRBUST

思路:

对于没有0出现的数字重组满足这样的条件就能使得输出的数字最小:从出现的最小的数开始输出,对应输出出现的次数,直到所有数字输出完毕。

对于有0出现的数字重组满足这样的条件就能使得输出的数字最小:先输出一个非0的最小的数字,然后输出0,剩下的部分按照没有0的规则输出,模拟代码直接AC:

#include<stdio.h>
#include<string.h>
using namespace std;
int vis[10];
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        char a[200];
        memset(vis,0,sizeof(vis));
        scanf("%s",a);
        for(int i=0;i<strlen(a);i++)
        {
            vis[a[i]-'0']++;
        }
        if(vis[0]==0)
        {
            for(int i=1;i<10;i++)
            {
                for(int j=1;j<=vis[i];j++)
                printf("%d",i);
            }
            printf("\n");
        }
        else
        {
            int f=0;
            for(int i=1;i<10;i++)
            {
                if(vis[i]!=0)
                {
                    printf("%d",i);
                    vis[i]--;
                    break;
                }
            }
            for(int i=1;i<=vis[0];i++)
            {
                printf("0");
            }
            for(int i=1;i<10;i++)
            {
                for(int j=1;j<=vis[i];j++)
                printf("%d",i);
            }
            printf("\n");
        }
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值