hrbust 1387 Shuffle the Digits【贪心】
来源:互联网 发布:rails application.js 编辑:程序博客网 时间:2024/04/27 13:48
Shuffle the DigitsTime Limit: 1000 MSMemory Limit: 65536 KTotal Submit: 165(68 users)Total Accepted: 75(66 users)Rating: Special Judge: NoDescription
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!
InputThere 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).
OutputFor each test case:
Line 1. Output the new integer that Leyni wants.
Sample Input1
4321
Sample Output1234
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
- hrbust 1387 Shuffle the Digits【贪心】
- TOJ 4493 Remove Digits 贪心
- pku3590 The shuffle Problem
- POJ3590 The shuffle Problem
- hrbust 1851加油站(贪心+广搜)
- hrbust 哈理工oj 1588 神医【贪心】
- hrbust哈理工oj 1674 充电【贪心】
- hrbust 1584 青蛙过河【二分查找+贪心】
- hrbust 1539 选课【思维+贪心】好题
- hrbust 1691 无聊的游戏【贪心】
- hrbust 1325 Leyni的车牌号【贪心】
- hrbust 1648 Tailmon与序列【贪心】
- Hrbust 1750 Eternal Victory【贪心+Dfs】
- hrbust 1079 I can do it(贪心)
- pku 3590 The shuffle Problem
- 【POJ 3590】The shuffle Problem
- 993 - Product of digits(贪心)
- UVA - 993 Product of digits 贪心
- 前缀++ 后缀++ 运算符重载
- 胜利大逃亡
- CentOS配置免密码SSH登录
- 大白话说期权——除了买涨买跌,我们还能怎么交易?二元期权又是什么鬼?
- 函数指针及其的运用(上)——何为函数指针
- hrbust 1387 Shuffle the Digits【贪心】
- Swift - 让程序挂起后,能在后台继续运行任务
- 安卓弹出对话框——Alertdialog(一)
- javascript继承机制综述
- 基于协同过滤的推荐引擎
- nginx lvs
- mysql学习书籍
- MFC截获键盘输入值
- 动态设置ViewPager允许/禁止滑动