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. ...
随机推荐
- php 冒泡法 排序
<?php /** * php 冒泡法 * @param $arr * @param string $order 排序符 * @return $arr */ function orderarr( ...
- 如何用SQL语句处理缓慢变化维(渐变维,拉链表)SCD-2?
假设有一张居民维表,需要记录居民状态的变更历史,根据Kimball建模理论,设计居民维表如下: 另外在ODS中有居民信息的每日快照表(每天都记录一份居民的全量信息):O_USERINFO 如何将ODS ...
- consonant_摩擦音
consonant_摩擦音_[t∫].[dʒ].[tr].[dr].[ts].[dz] 破擦音:即有爆破音又有摩擦音. [t∫]:噘嘴,舌尖抵住上牙龈,舌头下切,用一瞬间的气流发出声音,不震动. ch ...
- nodejs module/require
1. wrap up a module using module.exports, name the file to a.js var fun1=function(){ var stylus = re ...
- 北京Uber优步司机奖励政策(2月24日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- 北京Uber优步司机奖励政策(11月30日~12月4日)
用户组:人民优步(适用于12月1日)奖励政策: 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:htt ...
- 深圳Uber优步司机奖励政策(12月28日到1月3日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- Mac iTem2 自动登录服务器配置
假设你要连接的服务器地址为123.123.123.123,端口号为8888,用户名为root,密码为mimamima 编写shell文件"login_server.sh",并放置于 ...
- Java子类与父类之间的类型转换
1.向上转换 父类的引用变量指向子类变量时,子类对象向父类对象向上转换.从子类向父类的转换不需要什么限制,只需直接蒋子类实例赋值给父类变量即可,这也是Java中多态的实现机制. 2.向下转换 在父类变 ...
- flask源码走读
Flask-Origin 源码版本 一直想好好理一下flask的实现,这个项目有Flask 0.1版本源码并加了注解,挺清晰明了的,我在其基础上完成了对Werkzeug的理解部分,大家如果想深入学习的 ...