PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!
博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
http://www.cnblogs.com/chenxiwenruo/p/6789138.html
特别不喜欢那些随便转载别人的原创文章又不给出链接的
所以不准偷偷复制博主的博客噢~~
题意:给出n个数,求拼接后值最小的数是多少。
一开始就简单的把所有字符串先从小到大拍个序,然后拼接起来去掉前导零,
结果发现有个问题,比如下面两个
32 32321
如果按常规字符串比较,32是在32321前面。
然而,32321-32才是较小的方案
如何有个好的比较方法?
使得32>32321
采用了重复填充的方法,直到填满8位为止,因为题目中每个数字最多8位
即将32->32(323232)
321->321(32132)
这样的话,前面就大于后面的了。输出的时候还是输出原来的即可
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cmath>
#include <map>
#include <vector>
using namespace std;
const int maxn=; struct Node{
int len;
char str1[];
char str2[];
bool operator<(const Node tmp)const{ }
}node[maxn],rnode[maxn]; bool cmp(char*str1,char*str2){
if(strcmp(str1,str2)<=)
return true;
else
return false; }
bool cmpNode(Node a,Node b){
return cmp(a.str2,b.str2);
}
int main()
{
int n;
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%s",node[i].str1);
node[i].len=strlen(node[i].str1);
for(int j=;j<=/node[i].len;j++){
strcpy(node[i].str2+j*node[i].len,node[i].str1);
}
node[i].str2[]='\0';
}
sort(node,node+n,cmpNode);
//for(int i=0;i<n;i++){
// printf("%s ",node[i].str2);
//}
//printf("\n");
bool leadzero=false;
//反过来即是所求的最小数字
for(int i=;i<n;i++){
if(!leadzero){
for(int j=;j<node[i].len;j++){
if(node[i].str1[j]!=''){
leadzero=true;
printf("%s",node[i].str1+j);
break;
}
}
}
else{
printf("%s",node[i].str1);
}
}
//如果全是0
if(!leadzero)
printf("0\n");
return ;
}
该题有个更好的解法那就是自定义排序的时候,我return a+b<b+a
想法太妙了,不得不说cmp函数的强大,哎自己受思维局限了。
想法出处:
http://www.liuchuo.net/archives/2303
PAT甲题题解-1038. Recover the Smallest Number (30)-排序/贪心,自定义cmp函数的强大啊!!!的更多相关文章
- 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 分)(思维题,贪心)
1038 Recover the Smallest Number (30 分) Given a collection of number segments, you are supposed to ...
- 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)
题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...
- PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- PAT 1038 Recover the Smallest Number (30分) string巧排序
题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...
- 1038. Recover the Smallest Number (30) - 字符串排序
题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...
- 1038 Recover the Smallest Number (30)(30 分)
Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...
- PAT甲题题解-1053. Path of Equal Weight (30)-dfs
由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...
随机推荐
- October 12th 2017 Week 41st Thursday
Be happy for this moment. This moment is your life. 为这一刻感到高兴,这一刻是你的人生. Yesterday Tencent became Asia ...
- plsqldev、oracle插入中文乱码问题解决
最近在开发项目时用到Oracle数据库,需要手动插入一些数据,但插入中文的sql值时出现了乱码. 中文乱码就是编码不统一所导致的了,解决办法只需要把编码统一下即可解决了. 此种情况最大的可能就是客户端 ...
- html简单介绍(一)
什么是html HTML 是用来描述网页的一种语言.HTML 指的是超文本标记语言 (Hyper Text Markup Language)HTML 不是一种编程语言,而是一种标记语言 (markup ...
- 3.Solr4.10.3目录结构
转载请出自出处:http://www.cnblogs.com/hd3013779515/ 1.整个目录结构 (1)bin:是脚本的启动目录 (2)contrib:第三方包存放的目录 (3)dist:编 ...
- 关于Java集合类库中的几种常用队列
Java中几种常用的队列 阻塞队列与普通队列的区别在于,当队列是空的时,从队列中获取元素的操作将会被阻塞,或者当队列是满时,往队列里添加元素的操作会被阻塞.试图从空的阻塞队列中获取元素的线程将会被阻塞 ...
- vSphere虚拟化管理平台的功能
VMware vSphere® 提供全球领先的虚拟化平台,用于构建云计算基础架构. vSphere 有哪些功能 强大的计算能力 虚拟化 x86 服务器资源并将其聚合成逻辑池,用于分配多个工作负载. 网 ...
- ocv & derate & crpr
OCV: on-chip-variation 是指芯片在制造工艺P.工作电压V.环境温度T 等参数的局部变化情况下导致的 cell &net delay 变化,比如假设从clk 到两个reg ...
- 欢迎到我的新Blog!
https://winniechen.cn 里面的页面还不是很好看...争取改一下! 里面的题解大部分也会在这里更新! 谢谢各位捧场!
- 大数据入门第十二天——sqoop入门
一.概述 1.sqoop是什么 从其官网:http://sqoop.apache.org/ Apache Sqoop(TM) is a tool designed for efficiently tr ...
- 20155209林虹宇 Exp6 信息搜集与漏洞扫描
Exp6 信息搜集与漏洞扫描 1.信息收集 1.1通过DNS和IP挖掘目标网站的信息 whois查询 进行whois查询时去掉www等前缀,因为注册域名时通常会注册一个上层域名,子域名由自身的域名服务 ...