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 ...
随机推荐
- linux中dd命令详解
本文转自:https://www.cnblogs.com/yuanqiangfei/p/9138625.html 一.dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换. ...
- 简明了解apply()和call()
apply()和call()都是ES6语法的,并且都是函数的方法. function foo() { alert(this.name) } var obj = { name: '小明' } foo() ...
- 如何保障MySQL主从复制关系的稳定性?关键词(新特性、crash-safe)
一 前言 MySQL 主从架构已经被广泛应用,保障主从复制关系的稳定性是大家一直关注的焦点.MySQL 5.6 针对主从复制稳定性提供了新特性: slave 支持 crash-safe.该功能可以解决 ...
- Java代理类Proxy的用法
代理(proxy) 利用代理可以在运行时创建一个实现了一组给定接口的新类.这种功能只有在编译时无法确定需要实现哪个接口时才有必要使用. 何时使用代理 假设有一个表示接口的Class对象(有可能只包含一 ...
- WPF 隐藏式控件
没用Popup用的面板控件,全部代码使用xaml的触发器. 代码: <Grid> <DockPanel> <StackPanel Background=" Do ...
- elasticSearch查询(一)
**整理成sql格式来看懂elastic** 1.多个字段多个and查询 sql格式:select * from product where title = 'xxxx' and pid = 12 l ...
- 前端开发JS——数组
25.数组 1)声明数组: ①构造函数创建数组 var arr = new Array(); console.log(arr): //[] var arr = new Array(2 ...
- html跳转,获取get提交参数
html跳转到html页面,url后面携带参数,可以通过脚本获取到url?test=value地址后的参数. 1.more.html 携带参数跳转到list.html,get提交参数 2.list.h ...
- CTF必备技能丨Linux Pwn入门教程——ROP技术(上)
Linux Pwn入门教程系列分享如约而至,本套课程是作者依据i春秋Pwn入门课程中的技术分类,并结合近几年赛事中出现的题目和文章整理出一份相对完整的Linux Pwn教程. 教程仅针对i386/am ...
- maven 学习---使用Maven创建Web应用程序项目
在本教程中,我们将演示如何使用 Maven 创建一个 Java Web 项目(Spring MVC). 用到的技术/工具: Maven 3.3.3 Eclipse 4.3 JDK 8 Spring 4 ...