1038. Recover the Smallest Number (30)

时间限制
400 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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


提交代码

学习网址:http://blog.csdn.net/sinat_29278271/article/details/48047877

里面的传递性是可以证明的

 1 #include<cstdio>
 2 #include<algorithm>
 3 #include<iostream>
 4 #include<cstring>
 5 #include<queue>
 6 #include<vector>
 7 #include<cmath>
 8 #include<string>
 9 using namespace std;
10 vector<string> v;
11 bool cmp(string a,string b){
12     return a+b<b+a;
13 }
14 int main(){
15     //freopen("D:\\INPUT.txt","r",stdin);
16     int n,i;
17     scanf("%d",&n);
18     string s;
19     for(i=0;i<n;i++){
20         cin>>s;
21         v.push_back(s);
22     }
23     sort(v.begin(),v.end(),cmp);
24     s="";
25     for(i=0;i<n;i++){
26         s=s+v[i];
27     }
28     for(i=0;i<s.length();i++){
29         if(s[i]!='0'){
30             break;
31         }
32     }
33
34     if(i==s.length()){
35         printf("0\n");
36     }
37     else{
38         for(;i<s.length();i++){
39             cout<<s[i];
40         }
41         cout<<endl;
42     }
43     return 0;
44 }

转载于:https://www.cnblogs.com/Deribs4/p/4770206.html

pat1038. Recover the Smallest Number (30)相关推荐

  1. PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...

  2. 1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 进行排序,注意comp的写法: #include <iostream> #include <vector ...

  3. A1038 Recover the Smallest Number (30)

    Powered by:NEFU AB-IN Link 文章目录 A1038 Recover the Smallest Number (30) 题意 思路 代码 A1038 Recover the Sm ...

  4. 1038 Recover the Smallest Number (30分)

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

  5. 1038 Recover the Smallest Number (30 分)【难度: 中 / 知识点: 贪心 思维】

    https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 PAT上的这道题的数据有点弱,其它网站上的数 ...

  6. PAT (Advanced Level) 1038. Recover the Smallest Number (30)

    注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...

  7. PAT——Recover the Smallest Number

    Recover the Smallest Number 题目 AC代码 参考 知识点总结 题目 Given a collection of number segments, you are suppo ...

  8. PAT甲级:1038 Recover the Smallest Number

    题目描述: Given a collection of number segments, you are supposed to recover the smallest number from th ...

  9. PAT-A-1038 Recover the Smallest Number 【贪心】 【二刷】

    贪心算法,最核心的在于贪心策略上 Given a collection of number segments, you are supposed to recover the smallest num ...

最新文章

  1. 疫情严重!国内互联网公司上班时间汇总!
  2. 同济大学和东南大学计算机哪个好,东南大学和同济大学哪个好?选择哪个以后发展比较好?...
  3. 从数据库备份创建数据库
  4. 9.struts1.x中tiles框架的使用
  5. 遥控开关,4G远程控制增氧机,智慧农渔更轻松!
  6. html 适配 android,Android 版本适配 6~11
  7. divi模板下载_Java Math类静态double IEEEremainder(double divi,double divisor)的示例
  8. 减少浏览器的兼容性问题
  9. 程序的内存分配----变量在可执行文件中的内存区分配
  10. 深入理解JVM-java内存区域与内存溢出异常
  11. 2022年寒假ACM练习1
  12. Mac OS10.12 编译Android源码8.1
  13. 离散数学及其应用第八版纠正(p29):关于n皇后问题的SAT表示
  14. Snipaste截屏软件使用
  15. AT89C51使用DAC0832数模转换,波形发生器
  16. Redis学习笔记1-理论篇
  17. Windows11 右键菜单没有新建选项操作
  18. Centos下如何永久修改系统时间 hwclock
  19. JFreeChart| JFreeChart组合图表(Combined Charts)
  20. Disk Expert 3.6.1 可视化磁盘清理工具

热门文章

  1. 3.1 matlab数据的输入和输出
  2. tril--下三角矩阵的抽取
  3. 【解决办法】pandas画出时序数据(股票数据)横轴不是时间
  4. Sympy常见多个变量【一行代码创建】
  5. boostrap 鼠标滚轮滑动图片_Bootstrap幻灯片轮播图支持触屏左右手势滑动的实现方法...
  6. vue项目中使用element的dialog中引入ztree却不能初始化解决办法
  7. 吴裕雄--天生自然 高等数学学习:多元函数的概念
  8. jQuery操作css样式、属性、动画、节点
  9. HDU - 6082 度度熊与邪恶大魔王(背包变式)
  10. 【CF582E】Boolean Function 树形DP+FWT