博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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函数的强大啊!!!的更多相关文章

  1. 1038 Recover the Smallest Number (30分)(贪心)

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  2. PAT 甲级 1038 Recover the Smallest Number (30 分)(思维题,贪心)

    1038 Recover the Smallest Number (30 分)   Given a collection of number segments, you are supposed to ...

  3. pat 甲级 1038. Recover the Smallest Number (30)

    1038. Recover the Smallest Number (30) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHE ...

  4. 1038. Recover the Smallest Number (30)

    题目链接:http://www.patest.cn/contests/pat-a-practise/1038 题目: 1038. Recover the Smallest Number (30) 时间 ...

  5. PAT Advanced 1038 Recover the Smallest Number (30) [贪⼼算法]

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

  6. PAT 1038 Recover the Smallest Number (30分) string巧排序

    题目 Given a collection of number segments, you are supposed to recover the smallest number from them. ...

  7. 1038. Recover the Smallest Number (30) - 字符串排序

    题目例如以下: Given a collection of number segments, you are supposed to recover the smallest number from ...

  8. 1038 Recover the Smallest Number (30)(30 分)

    Given a collection of number segments, you are supposed to recover the smallest number from them. Fo ...

  9. PAT甲题题解-1053. Path of Equal Weight (30)-dfs

    由于最后输出的路径排序是降序输出,相当于dfs的时候应该先遍历w最大的子节点. 链式前向星的遍历是从最后add的子节点开始,最后添加的应该是w最大的子节点, 因此建树的时候先对child按w从小到大排 ...

随机推荐

  1. scss 使用

    SCSS 常用功能 https://www.cnblogs.com/guangzan/p/10547335.html 定义变量$my-color: #666; //定义变量$my-heihgt: 10 ...

  2. November 05th, 2017 Week 45th Sunday

    Do not pray for an easy life, pray for the strength to endure a difficult one. 不要祈求安逸的人生,祈求拥有撑过艰难的力量 ...

  3. MySQL5.7通过压缩包方式安装与配置

    首先下载MySQL5.7的压缩包:https://dev.mysql.com/downloads/mysql/5.7.html#downloads 1.解压缩到目标文件夹,解压后有许多文件,介绍一下用 ...

  4. tcp尽快尽快了

    看的撒积分卡拉斯的放假啊的顺口溜

  5. Cannot find module '../lib/utils/unsupported.js'

    运行npm run clean出错: throw err; ^ Error: Cannot find module '../lib/utils/unsupported.js' at Function. ...

  6. OpenCV——Brisk特征检测、匹配与对象查找

    检测并绘制特征点: #include <opencv2/opencv.hpp> #include <opencv2/xfeatures2d.hpp> #include < ...

  7. python面试一:python2与python3的区别一

    1.默认编码方式不同:py3用的是utf-8,变量名更为广泛.2.去除<>改用!=3加入as 和with关键字4./除法默认数据类型不同 py2 5/3=1 py3 5//3=15.去掉了 ...

  8. JS实现 类似图片3D效果

    其实这样的效果 目前很多网站上都有 其实以前也写过一个 只是当时代码只是为了实现而已,代码很乱,所以现在有业余时间研究了下,其实也并没有什么特殊地方 很类似于左右控制按钮切换图片的思路.效果如下: 可 ...

  9. grunt项目构建工具

    JS项目构建工具Grunt实践 一:下面来介绍下如何用grunt合并,压缩js文件.    大概步骤有如下:     1. 新建文件夹相对应的项目 比如文件名叫:gruntJs      2. 新建文 ...

  10. D. Jzzhu and Cities

    Jzzhu is the president of country A. There are n cities numbered from 1 to n in his country. City 1  ...