题目

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中数据库操作之 (FOR ALL ENTRIES IN )用法,比较难明白

    一.代码如下 二.查询结果如下 三.我们把it_spfli的内表内容添加两个 四.结果如下,查询的是所有的JFK和SFO机场. *&------------------------------ ...

  2. 浅谈Python 中 __getattr__与__getattribute__的区别

    __getattr__与__getattribute__均是一般实例属性截取函数(generic instance attribute interception method),其中,__getatt ...

  3. oracle中设置主键

    1.创建表 .创建表 create table "c_user"( "id" number primary key, "username" ...

  4. 目标检测评价标准(mAP, 精准度(Precision), 召回率(Recall), 准确率(Accuracy),交除并(IoU))

    1. TP , FP , TN , FN定义 TP(True Positive)是正样本预测为正样本的数量,即与Ground truth区域的IoU>=threshold的预测框 FP(Fals ...

  5. 大二暑假第六周总结--开始学习Hadoop基础(五)

    简单学习数据仓库HIVE HIVE是一个构建于Hadoop顶端的数据仓库工具 支持大规模数据存储,分析,具有良好的可扩展性 某种程度上可以看做是用户编程接口,本身不存储和处理数据 依赖分布式系统HDF ...

  6. javascript实现抽奖程序

    昨天开年会的时候看到一个段子说唯品会年会抽奖,结果大奖都被写抽奖程序的部门得了,CTO现场review代码. 简单想了一下抽奖程序的实现,花了十几分钟写了一下,主要用到的知识有数组添加删除,以及ES5 ...

  7. SQL的查询结果复制到Excel 带标题Head 有换行符导致换行错乱 的解决方案

    将SQL查询到的结果保存为excel有很多方法,其中最简单的就是直接复制粘贴了 1.带Head的复制粘贴 1)先左击红色区域实现选择所有数据 2)随后右击选择Copy with Headers  再粘 ...

  8. 自己整理的常用SQL Server 2005 语句、

    --创建数据库 create database 数据库 go --打开数据库 use 数据库 --删除数据库 drop database 数据库 Go --创建数据表 create table 数据表 ...

  9. tensorflow应用于手写数字识别(第二版)

    import tensorflow as tf from tensorflow.examples.tutorials.mnist import input_data#载入数据集 mnist = inp ...

  10. python yield 使用示例

    1.yield由于创建迭代器 def deal(): tmp = [] for i in range(20): tmp.append(i) if i % 4 == 0: yield tmp tmp = ...