PAT——Recover the Smallest Number
Recover the Smallest Number
- 题目
- AC代码
- 参考
- 知识点总结
题目
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 (≤104 ) 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
AC代码
#include<bits/stdc++.h>
using namespace std;bool cmp(string x,string y){string xy=x+y,yx=y+x;return xy<yx;
}int main()
{int n,len;cin>>n;string s[n];for(int i=0;i<n;i++){cin>>s[i];len+=s[i].length();}sort(s,s+n,cmp);string ans="";for(int i=0;i<n;i++)ans+=s[i];stringstream ss;int t;ss<<s[n-1];ss>>t;if(t==0){cout<<0;return 0;} int flag=1;for(int i=0;i<ans.length();i++){if(flag&&ans[i]=='0') continue;else flag=0;cout<<ans[i];}
}
参考
Recover the Smallest Number (30) - 字符串排序
知识点总结
- stringstream的相关用法
- 字符串排序
PAT——Recover the Smallest Number相关推荐
- PAT甲级1038 Recover the Smallest Number (30 分):[C++题解]贪心、排列成最小的数、字符串
文章目录 题目分析 题目来源 题目分析 来源:acwing 分析: 贪心: 对于字符串a和b,如果 a+b < b+a (这里+代表字符串中的连接)代表字典序更小.举例 a = 321 , b ...
- PAT甲级:1038 Recover the Smallest Number
题目描述: Given a collection of number segments, you are supposed to recover the smallest number from th ...
- pat1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 1038. Recover the Smallest Number (30)
1038. Recover the Smallest Number (30) 进行排序,注意comp的写法: #include <iostream> #include <vector ...
- 1038 Recover the Smallest Number (30分)
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- PAT-A-1038 Recover the Smallest Number 【贪心】 【二刷】
贪心算法,最核心的在于贪心策略上 Given a collection of number segments, you are supposed to recover the smallest num ...
- A1038 Recover the Smallest Number (30)
Powered by:NEFU AB-IN Link 文章目录 A1038 Recover the Smallest Number (30) 题意 思路 代码 A1038 Recover the Sm ...
- PAT (Advanced Level) 1038. Recover the Smallest Number (30)
注意前导零的消去. #include <iostream> #include <string> #include <sstream> #include <al ...
- 1038 Recover the Smallest Number (30 分)【难度: 中 / 知识点: 贪心 思维】
https://pintia.cn/problem-sets/994805342720868352/problems/994805449625288704 PAT上的这道题的数据有点弱,其它网站上的数 ...
最新文章
- python释放类对象_Python 基本功: 10. 面对对象-类 Class
- Spring Cloud Feign的两种使用姿势
- 项目: 用c++写一个养成类的游戏【QQ宠物】
- JAVA——基于HttpClient的获取帆软FineReport报表爬虫DEMO
- 【Qt】2D绘图之图形视图框架(一)
- 机器学习从入门到精通50讲(四)-实时数仓应用实践案例
- C++ override 关键字用法
- 度假式办公环境、值得拥有!珠海金山软件诚聘CV、C++、后端工程师
- 小小数据统计(柱状图、折线图、扇形图)
- C++之yaml-cpp开源库的使用笔记
- 一个批量转换文件编码的python脚本
- mysql组合索引与字段顺序
- js中定义变量加var与不加var的区别?
- 【数字信号调制】基于matlab二进制差分相移键控调制(2DPSK)【含Matlab源码 163期】
- Python语音转文字、音频切割、语音识别
- 开源车牌数据集CCPD介绍
- 杭州计算机及应用不可以落户,90%的城市取消落户限制!附杭州落户超详细攻略...
- 破解sublime 3207(2019.6.3更新)
- 基于vue+百度地图的多车实时运动及轨迹追踪实现(上帝视角篇)
- deepin系统 crontab 执行自动任务