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的更多相关文章

  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. PAT甲1038 Recover the smallest number

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  4. PAT甲级——A1038 Recover the Smallest Number

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  5. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

  6. PAT 1038 Recover the Smallest Number[dp][难]

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  7. 1038 Recover the Smallest Number (30 分)

    1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to r ...

  8. 1038. Recover the Smallest Number (30)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...

  9. PAT 1038 Recover the Smallest Number (30分) string巧排序

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

随机推荐

  1. 安装cronsun管理定时脚本

    1. cronsun 是一个分布式任务系统,单个结点和 *nix 机器上的 crontab 近似.支持界面管理机器上的任务,支持任务失败邮件提醒,安装简单,使用方便,是替换 crontab 一个不错的 ...

  2. zkfc的znode不存在的问题

    cd /soft/hadoop/logs/hadoop-centos-zkfc-s101.log发现: 2018-09-29 12:42:03,616 FATAL org.apache.hadoop. ...

  3. 网站用户行为分析——Linux的安装

    Linux的选择 在Linux系统各个发行版中,CentOS系统和Ubuntu系统在服务端和桌面端使用占比最高,网络上资料最是齐全,所以建议使用CentOS系统或Ubuntu. 一般来说,如果要做服务 ...

  4. 搭建Jupyter Notebook服务器

    昨天发了Jupyter的使用,补一篇Jupyter服务器的搭建~ 一.搭建Jupyter 1.安装 使用virtualenv建虚拟环境.在虚拟环境中安装jupyter.matplotlib等等需要的库 ...

  5. C++编译错误杂记

    目录 2018年12月23日 error: no matching function for call to ××× 2018年12月10日 error: expected ')' before '* ...

  6. (转载)PHP环境搭建-记录

    PHP环境搭建-记录   转于 http://jingyan.baidu.com/article/fcb5aff797ec41edaa4a71c4.html php5.5 做了大量的更新,在与apac ...

  7. 北京Uber优步司机奖励政策(12月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...

  8. day 6 敌机

    1.显示敌机 #-*- coding:utf-8 -*- import pygame import time from pygame.locals import * class HeroPlane(o ...

  9. Android Parcelable 源码解析

    大家都知道,要想在Intent里面传递一些非基本类型的数据,有两种方式,一种实现Parcelable,另一种是实现Serializable接口. 今天先不说Serializable 接口,只说Parc ...

  10. springboot之RMI的使用

    1.RMI 指的是远程方法调用 (Remote Method Invocation).它是一种机制,能够让在某个 Java虚拟机上的对象调用另一个 Java 虚拟机中的对象上的方法.可以用此方法调用的 ...