Source:

PAT A1038 Recover the Smallest Number (30 分)

Description:

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 (≤) followed by Nnumber 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

Keys:

  • 贪心

Code:

 /*
Data: 2019-07-23 18:47:04
Problem: PAT_A1038#Recover the Smallest Number
AC: 16:22 题目大意:
给几个数,求拼成的最小数
*/
#include<cstdio>
#include<string>
#include<iostream>
#include<algorithm>
using namespace std;
const int M=1e4+;
string s[M]; bool cmp(string a, string b)
{
return a+b < b+a;
} int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("Test.txt", "r", stdin);
#endif int n;
scanf("%d", &n);
for(int i=; i<n; i++)
cin >> s[i];
sort(s,s+n,cmp);
string ans="";
for(int i=; i<n; i++)
ans += s[i];
while(ans.size()> && ans[]=='')
ans.erase(,);
cout << ans; return ;
}

PAT_A1038#Recover the Smallest Number的更多相关文章

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

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

  2. 1038. Recover the Smallest Number (30) - 字符串排序

    题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...

  3. A1038. Recover the Smallest Number

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

  4. PAT甲1038 Recover the smallest number

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

  5. 1038 Recover the Smallest Number (30 分)

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

  6. 1038. Recover the Smallest Number (30)

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

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

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

  8. PAT 甲级 1038 Recover the Smallest Number

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

  9. pat1038. Recover the Smallest Number (30)

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

随机推荐

  1. MySQL coalesce函数用法说明(转)

    在mysql中,其实有不少方法和函数是很有用的,一个叫coalesce的,作用是将返回传入的参数中第一个非null的值,比如 SELECT COALESCE(NULL, NULL, 1); -- Re ...

  2. HTML5: HTML5 Canvas

    ylbtech-HTML5: HTML5 Canvas 1.返回顶部 1. HTML5 Canvas <canvas> 标签定义图形,比如图表和其他图像,您必须使用脚本来绘制图形. 在画布 ...

  3. Openstack组件部署 — keystone(domain, projects, users, and roles)

    目录 目录 前文列表 Create a domain projects users and roles domain projects users and roles的意义和作用 Create the ...

  4. 19. HTTP协议二:HTTP请求与响应、常见状态码

    HTTP请求与响应 HTTP请求 HTTP请求是指从客户端到服务器端的请求消息.HTTP请求主要由三部分构成,请求行.请求头(headers).body(请求数据). 上图是笔者用Charles抓包工 ...

  5. 接口调用post请求参数在body中

    package com.ynhrm.common.utils; import com.alibaba.fastjson.JSONObject; import lombok.Data; import o ...

  6. sql 查询库是否存在

    网上查了很多,但是都是不完整的,很多坑,后面终于摸索出来了:DROP DATABASE IF EXISTS 库名(不要加引号); 这句话的意思就是如果库存在,就删除库,然后再新建库就行了.

  7. syslog-ng收集日志+ELK平台搭建教程

    syslog-ng部署: 用于接收交换机输出的日志. syslog-ng安装很简单,可以顺便搜一下,文章有很多.我是yum直接安装的. syslog-ng配置: vi /etc/syslog-ng/s ...

  8. 每天一个Linux常用命令 ls命令

    ls:列出目录中的内容 -l  显示详细信息 -a 显示所有文件,包括隐藏文件 -i  显示inode -t :依时间排序,而不是用档名. -r :将排序结果反向输出,例如:原本档名由小到大,反向则为 ...

  9. shell script test指令的测试功能 &和&&,|和|| 区别 变量名赋值=号前后的空格问题(天坑)

    小程序告一段落,达到阶段性目标.下一步继续Linux的学习....脑子不够用啊...真费... 书中介绍..检测系统某些文件或者相关属性时,用test指令.. 例如.测试某个文档目录是否存在可以  t ...

  10. 列举 contentType: 内容类型(MIME 类型)

    常用的: 1.".doc"="application/msword" 2.".pdf"="application/pdf" ...