题目

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 diferent 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 (<=10000) 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. Do not output leading zeros.

Sample Input:

5 32 321 3214 0229 87

Sample Output:

22932132143287

题目分析

已知一系列数字段,顺序可以随意调换,求最后组合成的最小数字(打印需要去掉前导0)

解题思路

  1. 对所有数字段排序,排序技巧有技巧性(s1+s2<s2+s1,排序结束后所有s1,s2对都是取s1+s2,所以整个数组s1+s2...sk组合为最小数字)

该证明来自晴神笔记P163

  1. 将排序处理完的所有数字段拼接,擦除前导0(擦除技巧有技巧性)

易错点

  1. 特殊情况:所有数字段都为0,擦除前导0后,可能最终字符串为空,需要输出0

Code

#include <iostream>
#include <algorithm>
using namespace std;
bool cmp(string &s1,string &s2) {
return s1+s2<s2+s1;
}
int main(int argc, char * argv[]) {
int N;
scanf("%d",&N);
string ss[N];
for(int i=0; i<N; i++) cin>>ss[i];
// 排序
sort(ss,ss+N,cmp);
// 获取字符串结果
string rs;
for(int i=0; i<N; i++) rs+=ss[i];
// 擦除前导0
while(rs.length()!=0&&rs[0]=='0')rs.erase(rs.begin());
// 若rs都为0,擦除完后,长度为0;
if(rs.length()==0)printf("0");
else printf("%s",rs.c_str());
return 0;
}

PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]的更多相关文章

  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. 1038. Recover the Smallest Number (30)

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

  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. PAT 1038 Recover the Smallest Number (30分) string巧排序

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

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

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

  7. PAT 甲级 1038 Recover the Smallest Number

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

  8. 1038 Recover the Smallest Number (30)(30 分)

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

  9. 1038 Recover the Smallest Number (30分)(贪心)

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

随机推荐

  1. 六十九、SAP中内表插入的三种方法之三,INSERT的使用,用于指定位置插入

    一.代码如下 二.需要注意的时候,如果内表和工作区同名,这可以用隐式插入,不需要什么工作区INTO到什么表,INDEX为位置,效果图如下:

  2. XML 之 语法详解

    一.文档规则 .区分大小写. .属性值必须加引号(单引号.双引号都可以),一般情况下建议使用使用双引号. .所有标记必须有结束符号. .所有空标记必须关闭. .必须有且仅有一根元素. .解析空白字符时 ...

  3. Elasticsearch 使用集群 - 创建索引

    章节 Elasticsearch 基本概念 Elasticsearch 安装 Elasticsearch 使用集群 Elasticsearch 健康检查 Elasticsearch 列出索引 Elas ...

  4. check Linux文件夹个数

    (ls -l|grep "^-"| wc -l)查看某个文件夹下文件的个数.(ls -lR|grep "^-"| wc -l)查看某个文件夹下文件的个数,包括子 ...

  5. 面向对象-main函数

    面向对象-main函数 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.   一.编写main函数测试代码 /** * * @author 尹正杰 * */ public class ...

  6. 【Android】家庭记账本手机版开发报告三

    一.说在前面 昨天 对第一天的框架结构进行了四方面的完善 今天 对界面显示和逻辑结构进行完善 问题 无 二.界面展示完善 1.使用可回收的列表recyclerView展示账单的信息,并设置数据项为卡片 ...

  7. POJ 1584:A Round Peg in a Ground Hole

    A Round Peg in a Ground Hole Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5741   Acc ...

  8. 17 ~ express ~ 分类的显示 ,修改 和 删除

    一,前台显示 /views/admin/category.html {% extends 'layout.html' %} {% block main %} <ol class="br ...

  9. 1 ~ express ~ 初始化。安装第三方模块express。中间件

    一,初始化 二,安装第三方模块express 三,安装中间件 1,bodyParser : 解析 post 请求数据 2,cookies : 读写 cookie 3,swig :模板解析引擎 4,mo ...

  10. Git--rebase合并提交

    参考 https://blog.csdn.net/hj7jay/article/details/78809547 https://blog.csdn.net/yangcs2009/article/de ...