A1038. Recover the Smallest Number
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 (<=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
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
string str[], ans;
bool cmp(string a, string b){
return a + b < b + a;
}
int main(){
int N, index = -;
scanf("%d", &N);
for(int i = ; i < N; i++)
cin >> str[i];
sort(str, str + N, cmp);
for(int i = ; i < N; i++){
ans += str[i];
}
int i = ;
while(ans[i] != '\0' && ans[i] == ''){
index = i;
i++;
}
if(index != -)
ans.erase(, index + );
if(ans.length() == )
cout << "";
else cout << ans;
cin >> N;
return ;
}
总结:
1、题意:将若干字符串拼接起来,使得组成的数字最小。如果直接按照字典序由小到大排列再拼接是不对的。正确做法:比较串a和串b的位置关系时应考虑,如果a+b < b + a,则说明a前b后所组成的数字更小。
2、输出结果要去掉前导0,但不能仅仅对str[0]去0,因为有可能存在str[N] = {"0000", "0000", "0000"}的情况,应全部拼接起来后统一去除0,当最终得到的字串长度为0时,需要人为输出一个0。
3、string去除子串:str.erase(首位置, 长度)
A1038. Recover the Smallest Number的更多相关文章
- PAT甲级——A1038 Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- A1038 Recover the Smallest Number (30 分)
一.技术总结 此问题是贪心类问题,给出可能有前导零的数字串,将他们按照某个顺序拼接,使生成的数最小. 解决方案,就是使用cmp函数,因为两两字符串进行拼接,进行排序从小到大. 拼接过后会有0可能出现在 ...
- PAT_A1038#Recover the Smallest Number
Source: PAT A1038 Recover the Smallest Number (30 分) Description: Given a collection of number segme ...
- 1038 Recover the Smallest Number (30 分)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 把数组排成最小的数/1038. Recover the Smallest Number
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. Give ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
- PAT甲1038 Recover the smallest number
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 1038. Recover the Smallest Number (30)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT 1038 Recover the Smallest Number[dp][难]
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
随机推荐
- Windows环境下实现Consul服务注册和服务发现
1.首先从官方网站下载Consul,因为我们是使用的Windows系统,所以选择windows版本 https://www.consul.io/downloads.html 2.可以用开发者模式来启动 ...
- 安装Visual Studio 2013以及简单使用
首先,在网上找到安装Visual Studio 2013的教程以及相关软件资源http://jingyan.baidu.com/article/09ea3ede3b2496c0afde3944.htm ...
- Record for Individual Project ( Word frequency program )
1. 预计时间 ● 对问题总体的理解.规划:10 min ● 设计编写程序:5 h ● 调试: 分模块-40 min; 总体-40min ● 测试(性能分析).改进:1 h 2. 实际用时 ● 对 ...
- mooc linux学习总结
通过八周的学习获得了很多知识. 首先,通过网课老师形象生动的讲述和描述一些专业词汇,使我更加深刻的记住并掌握了这些内容:动态的展示堆栈的变化,更容易理解一段汇编代码:分析操作系统的工作,记 ...
- Linux内核分析第四章 读书笔记
Linux内核分析第四章 读书笔记 第一部分--进程调度 进程调度:操作系统规定下的进程选取模式 面临问题:多任务选择问题 多任务操作系统就是能同时并发地交互执行多个进程的操作系统,在单处理器机器上这 ...
- github的使用心得
我的github地址:https://github.com/gaino1/test GitHub 是一个用于使用Git版本控制系统的项目的基于互联网的存取服务. GitHub可以托管各种git库,并提 ...
- 小学生四则运算App实验成果
组名:会飞的小鸟 组员:徐侃 陈志棚 罗伟业 刘芮熔 成员分工: ①刘芮熔:设置安卓包.界面的代码,界面的排序. ②陈志棚:加减乘除的判断异常处理,例如除数不能为零的异常处理等问题. ③徐侃 ...
- HDOJ2004_成绩转换
水题:用数组标识各个阶段分数的等级即可. HDOJ2004_成绩转换 #include<stdio.h> #include<stdlib.h> #include<math ...
- PAT 1009 说反话
https://pintia.cn/problem-sets/994805260223102976/problems/994805314941992960 给定一句英语,要求你编写程序,将句中所有单词 ...
- how-is-docker-different-from-a-normal-virtual-machine[Docker与VirtualMachine的区别]
https://stackoverflow.com/questions/16047306/how-is-docker-different-from-a-normal-virtual-machine 被 ...