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 ...
随机推荐
- 在macOS苹果电脑上安装Azure DevOps Server(TFS)代理
1. 概述 MacOS是一套运行于苹果Macintosh系列电脑上的操作系统,是首个在商用领域成功的图形用户界面操作系统.Iphone应用软件的开发人员,都使用运行macOS的电脑或mini盒子进行软 ...
- spring boot 开启https
1.生成证书 keytool -genkey -alias tomcat -keyalg RSA -keystore E:/https.keystore 将生成好的证书放在项目根目录即可 2 修改配置 ...
- SpringBoot入门-SpringBoot性能优化
SpringBoot启动优化 显示声明扫包范围: 即不使用@SpringBootApplication默认扫包,使用@ComponentScan(basePackages = { "com. ...
- Mysql 常用数据类型 占用字节数 [转]
数据类型是定义列中可以存储什么数据以及该数据实际怎么存储的基本规则.Mysql的常用数据类型主要有: 串数据类型:最常用的数据类型,有两种基本的串类型:分别为定长串和不定长串.定长串结束长度固定的字符 ...
- Nginx反向代理YUM请求
一.安装配置Nginx服务(Nginx服务器上建议先关闭iptables/firewalld服务,待实验完成后再根据实际情况配置) [root@localhost ~]# yum install ng ...
- Window权限维持(六):BITS Jobs
Windows操作系统包含各种实用程序,系统管理员可以使用它们来执行各种任务.这些实用程序之一是后台智能传输服务(BITS),它可以促进文件到Web服务器(HTTP)和共享文件夹(SMB)的传输能力. ...
- Maven配置教程详解
Maven的安装与配置 一.在https://www.cnblogs.com/zyx110/p/10799387.html中下载以下maven安装包 解压缩即可 根据你的安装路径配置maven环境变量 ...
- 有两个CIDR地址块208.128/11和208.130.28/22。是否有那一个地址块包含了另一个地址?如果有,请指出,并说明理由。
有两个CIDR地址块208.128/11和208.130.28/22.是否有那一个地址块包含了另一个地址?如果有,请指出,并说明理由. 208.128/11的前缀为:11010000 100: 208 ...
- 开发技术--Python核心知识A
开发|Python核心知识A A篇,主要介绍Python基础中列表,元祖,字典,集合,字符串,I/O,条件与循环,异常处理的底层知识与使用的注意事项. 希望大家对于Python会有新的收获,本篇不同于 ...
- redis 配置及编写启动脚本
#!/bin/sh # # Simple Redis init.d script conceived to work on Linux systems # as it does use of the ...