A1038 Recover the Smallest Number (30 分)
一、技术总结
- 此问题是贪心类问题,给出可能有前导零的数字串,将他们按照某个顺序拼接,使生成的数最小。
- 解决方案,就是使用cmp函数,因为两两字符串进行拼接,进行排序从小到大。
- 拼接过后会有0可能出现在最前面,需要借助s.erase(s.begin())进行去除字符串前面的'0'字符,如果是字符串长度不为0,然后第一个字符为'0',去除,最后进行判断去除后是否为空,如果是则直接输出0,如果不是那么输出拼接后的字符串。
- 从这题可以得知,字符串拼接后也可以直接进行比较大小。拼接直接使用
+号 - 去除字符串前的字符
0,可以使用s.erase()
二、参考代码
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
bool cmp(string a, string b){
return a + b < b + a;
}
string str[10010];
int main(){
int N;
string s;
cin >> N;
for(int i = 0; i < N; i++){
cin >> str[i];
}
sort(str, str+N, cmp);
for(int i = 0; i < N; i++){
s += str[i];
}
while(s.length() != 0 && s[0] == '0'){
s.erase(s.begin());
}
if(s.length() == 0){
cout << 0;
}
cout << s;
return 0;
}
A1038 Recover the Smallest Number (30 分)的更多相关文章
- PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- 1038 Recover the Smallest Number (30分)(贪心)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 【PAT甲级】1038 Recover the Smallest Number (30 分)
题意: 输入一个正整数N(<=10000),接下来输入N个字符串,每个字符串包括至多8个字符,均为数字0~9.输出由这些字符串连接而成的最小数字(不输出前导零). trick: 数据点0只包含没 ...
- 1038. Recover the Smallest Number (30)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- pat1038. 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) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...
- 1038 Recover the Smallest Number (30)(30 分)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
随机推荐
- Debug 路漫漫-15:Python: NameError:name 'dataset' is not defined
在调试 <Outer Product-based Neural Collaborative Filtering>论文的源码(https://github.com/duxy-me/ConvN ...
- Redis 设计与实现,看 SDS(Simple Dynamic String) 感悟
Redis 设计与实现,看 SDS(Simple Dynamic String) 感悟 今天在看 Redis 设计与实现这本书的时候,发现了里面系统定义的数据结构 SDS,中文名为 简单动态字符串.对 ...
- Elasticsearch 7.x从入门到精通
Elasticsearch是一个分布式.可扩展.近实时的搜索与数据分析引擎,它能从项目一开始就赋予你的数据以搜索.分析和探索的能力. 通过本专栏的学习,你可以了解到,Elasticsearch在互联网 ...
- docker安装kafka
文本摘自此文章 .kafka需要zookeeper管理,所以需要先安装zookeeper. 下载zookeeper镜像 $ docker pull wurstmeister/zookeeper .启动 ...
- My time is limited
Your time is limited, so don't waste it living someone else's life. Don't be trapped by dogma - whic ...
- Winform 通过 WebBrowser 与 JS 交互
Winform 通过 WebBrowser 与 JS 交互 魏刘宏 2019.08.17 之前在使用 Cef (可在 Winform 或 WPF 程序中嵌入 Chrome 内核的网页浏览器的组件)时, ...
- ASP.NET Core 开发人员异常页面
UseDeveloperExceptionPage 中间件 我们谈谈在 Startup 类的 Configure()方法中以下代码: public void Configure(IApplicatio ...
- CentOS7 vsftp 安装与配置(视频教程)
(双击全屏播放) 1.安装vsftpd yum install -y vsftpd 2.编辑ftp配置文件 vi /etc/vsftpd/vsftpd.conf anonymous_enable=NO ...
- 使用highcharts实现无其他信息纯趋势图实战实例
使用highcharts实现无其他信息纯趋势图实战实例 Highcharts去掉或者隐藏掉y轴的刻度线yAxis : { gridLineWidth: 0, labels:{ //enabled:fa ...
- charles代理设置与数据劫持
1.安装charles,点击帮助——ssl代理——在移动设备或远程浏览器上安装charles root证书,看到如下界面: 2.在手机保证和电脑连接同一个wifi的前提下,开启手机代理,输入服务器地址 ...