PAT 甲级 1038 Recover the Smallest Number
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704
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 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
代码:
#include <bits/stdc++.h>
using namespace std; const int maxn = 1e5 + 10;
int N; struct Node{
string s;
}node[maxn]; bool cmp(const Node& a, const Node& b) {
return a.s + b.s < b.s + a.s;
} int main() {
scanf("%d", &N);
for(int i = 0; i < N; i ++)
cin >> node[i].s;
sort(node, node + N, cmp);
string ans = "";
for(int i = 0; i < N; i ++)
ans += node[i].s;
int len = ans.length();
int cnt = 0, temp = 0;
for(int i = 0; i < len; i ++) {
if(ans[i] != '0') {
temp = i;
break;
}
else cnt ++;
}
if(cnt == len)
printf("0");
else {
for(int i = temp; i < len; i ++)
cout << ans[i];
}
printf("\n");
return 0;
}
之前好像在 LeetCode 上做过类似的题目
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
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...
- PAT甲级——A1038 Recover the Smallest Number
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- 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. ...
随机推荐
- oracle 用户尝试登录失败锁定策略及修改
-- 修改密码的有效期策略, 永不过期SQL> ALTER PROFILE DEFAULT LIMIT PASSWORD_LIFE_TIME UNLIMITED;Profile altered ...
- Struts2+EasyUI+Hibernate小实例
概述 这个实例主要是前台数据到后台数据的传递和后台数据到前台数据的传递,完成数据的新增,以及对新增数据的展示.下面是详细的过程: Hibernate(数据库部分) 这里只是数据库的连接和数据库实体与物 ...
- PADS随记
在PADS,PCB板设计中,怎么一次就把丝印的大小设置好? CTRL+ALT+F 组合键 打开 选择过滤器(Filter) 如下图 去掉其他的勾选,只选择 Labels . 之后在板子上鼠标拖动选上 ...
- Qt上FFTW組件的编译与安裝
Qt上FFTW組件的編譯安裝 FFTW是一個做頻譜非常實用的組件,本文講述在Windows和Linux兩個平臺使用FFTW組件.Windows下的的FFTW組件已經編譯好成爲dll文件,按照開發應用的 ...
- Python学习:10.Python装饰器讲解(一)
情景介绍 一天,在你正在努力加班的时候,老板给交给你了一个任务,就是在这段代码里将所有函数开始输出一个‘hello’最后输出当前时间,再输出一个“end”,这段代码里包含了大量的函数,你会怎么做? d ...
- Python 爬虫 (二)
cookiejar模块: 管理储存cookie,将传出的http请求添加cookie cookie存储在内存中,CookieJar示例回收后cookie将自动消失 实例:用cookjar访问人人网主页 ...
- 数据结构中的hash
最近接触数据结构的时候突然发现一直在使用哈希表,哈希算法.那么到底什么是哈希(hash).查找资料发现一个比较有意思的解释,在此分享一下. 人家说的很好我就直接粘过来. =============== ...
- 20145209刘一阳《网络对抗》实验五:MSF基础应用
20145209刘一阳<网络对抗>实验五:MSF基础应用 主动攻击 首先,我们需要弄一个xp sp3 English系统的虚拟机,然后本次主动攻击就在我们kali和xp之间来完成. 然后我 ...
- 天津市人民优步Uber司机奖励政策(9月14日~9月20日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Web服务器、Web容器、Application服务器、反向代理服务器的区别与联系
在Web开发中,经常会听到Web服务器(Web Server).Web容器(Web Container).应用服务器(Application Server).反向代理服务器(Reverse Proxy ...