PAT甲1038 Recover the smallest number
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 (≤104) 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的更多相关文章
- pat 甲级 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- PAT 甲级 1038 Recover the Smallest Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 Given a collection of ...
- PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- PAT 1038 Recover the Smallest Number[dp][难]
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- 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 (30)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 把数组排成最小的数/1038. Recover the Smallest Number
题目描述 输入一个正整数数组,把数组里所有数字拼接起来排成一个数,打印能拼接出的所有数字中最小的一个.例如输入数组{3,32,321},则打印出这三个数字能排成的最小数字为321323. Give ...
随机推荐
- Floyd算法解说
開始知道Floyd算法是在<大话数据结构>这本书的无向带权图求最短路径看到的, 可是第一次没怎么看懂,所以就不看了,后来又看了两遍还是没明确,我以为是我理解能力有问题 后来从百度百科上看了 ...
- 产品半夜发现bug让程序员加班,程序员应如何回应?
群友半夜2点被产品叫起来改东西,然后他打车去公司加班,群友愤慨,特为此创作一个小段子 产品:XXX出bug了你赶紧看看 修复一下 开发:我宿舍没电脑 没网络啊 产品:你看看周围有没有网吧远程一下 或者 ...
- Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾
Mybatis数据库连接报错:对实体 "characterEncoding" 的引用必须以 ';' 分隔符结尾 ============================== 蕃薯耀 ...
- Struts2开发模式漏洞
当Struts2中的devMode模式设置为true时,存在严重远程代码执行漏洞.如果WEB服务以最高权限运行时,可远程执行任意命令,包括远程控制服务器. 如果为受影响的版本,建议修改配置文件stru ...
- mongo数据库查询结果不包括_id字段方法
db.GPRS_PRODUCT_HIS_FEE.find({"条件字段" : "412171211145135"},{_id:0}) db.GPRS_PRODU ...
- Kali linux 试用:dnsenum
dnsenum的目的是尽可能收集一个域的信息,它能够通过谷歌或者字典件猜测可能存在的域名,以及对一个网段进行反向查询.它可以查询网站的主机地址信息.域名服务器.mx record(函件交换记录),在域 ...
- 执行Batch批处理遇到的问题
1.务必关掉自动提交 增强执行效率 conn.setAutoCommit(false); 2.executeBatch失效问题 <1>务必将语句pstmt = conn.prepareSt ...
- WP8.1学习系列(第十章)——中心控件Hub设计指南
Windows Phone 应用商店应用中的中心控件指南 在本文中 说明 示例 用法指南 设计指南 相关主题 重要的 API Hub (XAML) HubSection (XAML) 说明 中心控 ...
- 【十大算法实现之KNN】KNN算法实例(含测试数据和源码)
KNN算法基本的思路是比较好理解的,今天根据它的特点写了一个实例,我会把所有的数据和代码都写在下面供大家参考,不足之处,请指正.谢谢! update:工程代码全部在本页面中,测试数据已丢失,建议去UC ...
- springboot---->springboot中的格式化(一)
这里面我们简单的学习一下springboot中关于数据格式化的使用.我以为你不是个好人,没想到你连个坏人都不是. springboot中的格式化 我们的测试环境是springboot,一个将字符串格式 ...