博主欢迎转载,但请给出本文链接,我尊重你,你尊重我,谢谢~
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. 解决Maven下载慢的问题

    直接在pom.xml中添加阿里的镜像 <repositories> <repository> <id>aliyun</id> <name>a ...

  2. Android 生态消息推送平台介绍

    一.手机厂商平台 华为消息推送服务 华为推送(Push)是为开发者提供的消息推送平台,建立了从云端到手机端的消息推送通道,使应用可以将最新信息及时通知用户,从而构筑良好的用户关系,提升用户的感知和活跃 ...

  3. BZOJ 1028 麻将 枚举

    题目链接: https://www.lydsy.com/JudgeOnline/problem.php?id=1028 题目大意: 麻将是中国传统的娱乐工具之一.麻将牌的牌可以分为字牌(共有东.南.西 ...

  4. 有crontab中的脚本不执行,需要在脚本里面export各种环境变量

    [oracle@sta ~]$ vi .bash_profile # .bash_profile # Get the aliases and functionsif [ -f ~/.bashrc ]; ...

  5. rpm安装MySQL5.5后配置,在centos5上;mysql编译安装在centos6.5上;

    [1] 没有/etc/my.cnf: rpm包安装的MySQL是不会安装/etc/my.cnf文件的:处理:cp /usr/share/mysql/my-huge.cnf /etc/my.cnf [2 ...

  6. OpenCV——边缘检测(sobel算子、Laplacian算子、scharr滤波器)

    #include <opencv2/opencv.hpp> #include <iostream> using namespace cv; using namespace st ...

  7. OpenCV——图像金字塔和图片尺寸缩放

  8. Linux内核RPC请求过程

    这篇文章讲讲server端RPC报文的处理流程.server端RPC报文的处理函数是svc_process,这个函数位于net/sunrpc/svc.c中.这个函数须要一个svc_rqst结构的指针作 ...

  9. 福慧双修&探险 BZOJ4398&BZOJ2407

    分析: 双倍经验(数据范围不同). 我们考虑,我们必定是从1走一条边到节点i,之后从i到j跑最短路,之后再从j到1走另一条边的情况下,不会重复,并且是答案.那么我们考虑预处理出pre[i]表示从1走到 ...

  10. jpbm工作流框架

    一:JBPM是什么?有什么用?能解决什么问题? 现实生活中有很多需要走一些流程的过程,比如请假流程,报销流程等,使用工作流框架,即可写一个流程即可,添加流程时不在繁琐的建立新的各种配置. 1:jBPM ...