1038 Recover the Smallest Number (30 分)

Given a collection of number segments, you are supposed to recover the smallest number from them. For example, given { 32, 321, 3214, 0229, 87 }, we can recover many numbers such like 32-321-3214-0229-87 or 0229-32-87-321-3214 with respect to different orders of combinations of these segments, and the smallest number is 0229-321-3214-32-87.

Input Specification:

Each input file contains one test case. Each case gives a positive integer N (≤10​4​​) followed by N number segments. Each segment contains a non-negative integer of no more than 8 digits. All the numbers in a line are separated by a space.

Output Specification:

For each test case, print the smallest number in one line. Notice that the first digit must not be zero.

Sample Input:

5 32 321 3214 0229 87

Sample Output:

22932132143287

题意:

给定n个数,要求任意组合使得组合得到的数最小。

思路:

之前做过一道类似的。这道题给的样例好啊。

表面上看好像是应该将输入的数字存成字符,按照字典序从小到大输出。但是观察样例会发现并不是这样的。

因为单个的字典序最小并不代表组合了之后的字典序最小。

应该要写一个cmp, 用的是组合之后字典序最小的排前面

还有要注意都是0的时候要记得最后输出一个0

 #include <iostream>
#include <set>
#include <cmath>
#include <stdio.h>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <map>
using namespace std;
typedef long long LL;
#define inf 0x7f7f7f7f const int maxn = 1e4 + ;
string strnum[maxn];
int n; bool cmp(string a, string b)
{
return a + b < b + a;
} int main()
{
scanf("%d", &n);
for(int i = ; i < n; i++){
cin>>strnum[i];
}
//while(cin>>strnum[n++]);
sort(strnum, strnum + n, cmp);
bool flag = true;
for(int i = ; i < n; i++){
int l = strnum[i].length();
for(int j = ; j < l; j++){
if(flag && strnum[i][j] == '')continue;
if(flag && strnum[i][j] != ''){
flag = false;
}
printf("%c", strnum[i][j]);
}
}
if(flag){
printf("");
}
printf("\n");
return ;
}

PAT甲1038 Recover the smallest number的更多相关文章

  1. pat 甲级 1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  2. PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)

    1038 Recover the Smallest Number (30 分)   Given a collection of number segments, you are supposed to ...

  3. PAT 甲级 1038 Recover the Smallest Number

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...

  4. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

  5. PAT 1038 Recover the Smallest Number[dp][难]

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  6. 1038 Recover the Smallest Number (30 分)

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  7. 1038. Recover the Smallest Number (30)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...

  8. PAT 1038 Recover the Smallest Number (30分) string巧排序

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

  9. 把数组排成最小的数/1038. Recover the Smallest Number

    题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323.   Give ...

随机推荐

  1. [原]IOS 设备基本信息

    1.获取设备类型  (Iphone/ipad 几?) #import "sys/utsname.h" -(NSString*)getDeviceVersion{    struct ...

  2. Linux+Redis实战教程_day01_常用命令【重点】

    3.常用命令[重点] Linux命令中参数,一般都是无序的.特殊情况下除外 3.1.磁盘管理命令 ls命令:列出目录内容 参数: -a 查询所有文件和文件夹.包含隐藏的 -l 查询详细列表    ls ...

  3. ios开发之--把秒转换为天时分秒

    把秒转换成时分秒: - (NSString *)timeFormatted:(int)totalSeconds { ; ) % ; ; return [NSString stringWithForma ...

  4. PostgreSQL的表空间

    1. 表空间的概念 PostgreSQL中的表空间允许在文件系统中定义用来存放表示数据库对象的文件的位置.在PostgreSQL中表空间实际上就是给表指定一个存储目录. 2. 表空间的作用 官方解释 ...

  5. Spring中神奇@aotuWrited

    好久没有写博客了,放假就是充电学习的时候,的确一直是这样做的.来给自己一点掌声.我们还是进入今天的主题吧. 我们自己写代码一般会向下面这样干啊,因为这样简单,其余交给spring去做吧.Spring会 ...

  6. IIS 7安装ModSecurity实现WAF功能

    ModSecurity 是一款开源Web应用防火墙,支持Apache/Nginx/IIS,可作为服务器基础安全设施,还是不错的选择. 系统环境:window 2008 R2+IIS 7 0X01 Mo ...

  7. Win10 我的电脑 -- 右键点击管理打不开

    右键点击我的电脑 -- 管理,出现如下错误,这是删除快捷方式小箭头导致的 解决方法: win+R 输入 regedit,分别在 HKEY_CLASSES_ROOT\piffile HKEY_CLASS ...

  8. 51单片机的idata,xdata,pdata,data的详解(转)

    data: 固定指前面0x00-0x7f的128个RAM,可以用acc直接读写的,速度最快,生成的代码也最小. bit :是指0x20-0x2f的可位寻址区idata:固定指前面0x00-0xff的2 ...

  9. 微信小程序的图片懒加载

    在普通的web页面当中,我们都知道图片懒加载可以提升浏览器的加载速度.原理是图片用空或者占位图片进行显示,当屏幕移动到图片位置的时候,再把图片的地址换成它的地址.那么,在小程序当中呢,最近老大让看一下 ...

  10. 【转】java文件操作大全

    一.获得控制台用户输入的信息 public String getInputMessage() throws IOException...{         System.out.println(&qu ...