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 ...
 
随机推荐
- 探索ASP.Net Core 3.0系列四:在ASP.NET Core 3.0的应用中启动时运行异步任务
			
前言:在本文中,我将介绍ASP.NET Core 3.0 WebHost的微小更改如何使使用IHostedService在应用程序启动时更轻松地运行异步任务. 翻译 :Andrew Lock ht ...
 - (三十二)golang--面向对象之封装
			
封装:把抽象出来的字段和对字段的操作封装在一起,数据被保护在内部,程序的其它包只有通过被授权的操作(方法),才能对字段进行操作. 封装的好处: (1)隐藏实际的细节: (2)可以对数据进行验证,保证安 ...
 - redis命令之 ----List(列表)
			
BLPOP BRPOP BRPOPLPUSH LINDEX LINDEX key index 返回列表 key 中,下标为 index 的元素. 下标(index)参数 start 和 stop 都以 ...
 - 用openresty(Lua)写一个获取YouTube直播状态的接口
			
文章原发布于:https://www.chenxublog.com/2019/08/29/openresty-get-youtube-live-api.html 之前在QQ机器人上面加了个虚拟主播开播 ...
 - ADO.NET中的5个主要对象
			
1.Connection:主要是开启程序和数据库之间的连接.没有利用连接对象将数据库打开,是无法从数据库中取得数据的. Close和Dispose的区别,Close以后还可以Open,Dispose以 ...
 - Winform中设置ZedGraph的曲线为散点图
			
场景 Winform中设置ZedGraph的曲线符号Symbol以及对应关系: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/10 ...
 - C# 多线程处理List数据
			
代码思路 将要处理的数据放到ConcurrentQueue中,然后开启多个线程去处理数据,处理完成后,再到队列中获取下一个待处理数据. ConcurrentQueue 表示线程安全的先进先出 (FIF ...
 - Linux 配置程序包源 Nuget
			
编辑文件NuGet.Config vi ~/.nuget/NuGet/NuGet.Config 新增源 <add key="fz" value="http://19 ...
 - (九)分布式服务----Zookeeper注册中心
			
==>>点击查看本系列文章目录 首先看一下几种注册中心: 最老的就是Zookeeper了, 比较新的有Eureka,Consul 都可以做注册中心.可以自行搜索对比三者的优缺点. Zook ...
 - webpack4 plugins 篇
			
demo 代码点此,篇幅有限,仅介绍几个常用的. start 什么是 plugins ? While loaders are used to transform certain types of mo ...