【PAT Practise】1038. Recover the Smallest Number (30)
来源:互联网 发布:centos vim插件 编辑:程序博客网 时间:2024/06/13 04:28
【原题】 https://www.patest.cn/contests/pat-a-practise/1038
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
【思路】
1. 这题我想了半天,结果发现自己完全是想多了,结论特别简单;
2. 首先这些数字片段得存储为字符串,这样方便拼接和比较;
3. 其次两个字符串进行关系运算符比较的时候是在比它们的字母序,在这里这和这两个字符串所代表的值比大小是一毛一样的;
4. 所以这里只要这么做就行了:相邻数字片段a和数字片段b拼接为a+b字符串和b+a字符串,我们比较a+b字符串和b+a字符串(即比较其所代表的数值大小),不用担心别的数字片段的影响,想想你在一个很大的数中任意调换两个相邻片段导致的数值变化是不是和头尾的数位都没关系;
5. 对于任意两个相邻片段都做这样的操作:如果 a+b < b+a,则让a放在前面(高位),b放在后面(低位), 因为这样能让整体数值减小。反复进行这样的排序直到全部片段都按照最小数值的顺序排列,这个反复操作通过sort函数就可以搞定。
【代码】
#include <iostream>#include <algorithm>#include <vector>#include <string>using namespace std;int n;vector<string> num_segs;bool Comp(const string &a, const string &b){return a + b < b + a;}int main(){cin >> n;string seg;for (int i = 0; i < n; i++){cin >> seg;num_segs.push_back(seg);}sort(num_segs.begin(), num_segs.end(), Comp); //关键就是这一句话string out;for (int i = 0; i < num_segs.size(); i++)out += num_segs[i];bool is_zero = true;for (int i = 0; i < out.size(); i++){if (is_zero){if (out[i] != '0')is_zero = false;}if (!is_zero)cout << out[i];}if (is_zero)cout << 0;system("pause");return 0;}
【附】
最近浪了好几天,所以断了一周,1038题之前还有一些题目我也觉得挺烦的,回头再补上。
- 【PAT Practise】1038. Recover the Smallest Number (30)
- PAT (Advanced Level) Practise 1038 Recover the Smallest Number (30)
- PAT (Advanced Level) Practise 1038 Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)-PAT
- PAT 1038. Recover the Smallest Number (30)
- 【PAT】1038. Recover the Smallest Number (30)
- [pat]1038. Recover the Smallest Number (30)
- pat 1038. Recover the Smallest Number (30)
- PAT 1038. Recover the Smallest Number (30)
- 1038. Recover the Smallest Number (30)【排序】——PAT (Advanced Level) Practise
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number
- PAT 1038. Recover the Smallest Number
- 【PAT】1038. Recover the Smallest Number
- PAT--1038. Recover the Smallest Number
- PAT 1049Recover the Smallest Number (30)
- pat-a1038. Recover the Smallest Number (30)
- 数据库mariadb
- caffe层解析之softmaxwithloss层
- Raptor中统计不同的字符
- codeforces Round 447 div2 D Ralph And His Tour in Binary Country
- 倒立摆系统分析及控制
- 【PAT Practise】1038. Recover the Smallest Number (30)
- niagara架构入门级理解
- C语言基础二(函数)
- 搭建lua开发环境(mac&&linux)
- HTML标签<br><br/>的区别在哪里?
- Java小白之路
- Linux内核学习经验总结
- 小波变换教程(十三)
- 设计模式-策略模式