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 ...
随机推荐
- Pencil 基于Electron的GUI原型工具之菜单再探
为什么要重试呢? 主要是觉得Pencil这个工具还是比较有价值.就像Linus对Linux下分发版的态度"让用户有选择"一样,在现在这个Sass服务.Web服务化越来越普遍越便利的 ...
- 在Azure DevOps Server (TFS)的流水线中编译和测试Xcode移动应用(iPhone)
概述 Xcode是开发基于苹果macOS系统的桌面应用和移动应用的主要IDE工具.使用Azure DevOps Server (原名TFS)系统中的pipelines流水线功能,可以方便的集成Xcod ...
- HTML连载21-序选择器上
解释CSS3 中新增的选择器中最具有代表性的就是序选择器,大致可以分为两类: (1)同级别的第几个(2)同类型的第几个 先写一个公共代码 <body> <h1>优秀</h ...
- [MFC]_在vs2019中使用MFC快速构建简单windows窗口程序
微软基础类库(英语: Classes,简称MFC)是微软公司提供的一个类库(class libraries),以C++类的形式封装了Windows API,并且包含一个应用程序框架,以减少应用程序开发 ...
- mysql-新增数据表
新增数据表之前,需确保已经存在数据库,如还没有数据库请先参考上一篇文章新增数据库 1.创建表 create table test( id int PRIMARY KEY, name varcha ...
- jQuery 源码分析(四) each函数 $.each和$.fn.each方法 详解
$.each一般用来遍历一个数组或对象,$.fn.each()就是指jQuery实例可以执行的操作(因为$.fn是jQuery对象的原型) $.each用来遍历一个数组或对象,并依次执行回掉函数,最后 ...
- linux部署java命令
启动: #!/bin/bash source /etc/profile log() { echo `date +[%Y-%m-%d" "%H:%M:%S]` $1 } log &q ...
- wpf的控件树
转载自:控件树
- python3之本地文件模拟登录
本地文件模拟登录 user.txt文本文件数据格式: username:password #coding:utf-8 #读取数据 userFile = open("user.txt" ...
- 深入浅出JVM的锁优化案例
锁优化 适应性自旋(Adaptive Spinning) 线程阻塞的时候,让等待的线程不放弃cpu执行时间,而是执行一个自旋(一般是空循环),这叫做自旋锁. 自旋等待本身虽然避免了线程切换的开销,但它 ...