一组正整数数字,求这组数字能组成的最大数?有没有好解法?

shfodhifdhiauh 2014-10-21 09:00:17
RT一组正整数数字,求这组数字能组成的最大数?有没有好解法?
...全文
596 40 打赏 收藏 转发到动态 举报
写回复
用AI写文章
40 条回复
切换为时间正序
请发表友善的回复…
发表回复
shfodhifdhiauh 2014-10-23
  • 打赏
  • 举报
回复
引用 29 楼 liuxie99 的回复:
我也觉得应该是a515360208说的 “要把每个数的高位到低位都分开了进行排序,最高位相同的比较次高位,依次类推,”
次高位没有比如,121和12谁大
shfodhifdhiauh 2014-10-23
  • 打赏
  • 举报
回复
引用 28 楼 nice_cxf 的回复:
[quote=引用 27 楼 shfodhifdhiauh 的回复:] [quote=引用 26 楼 nice_cxf 的回复:] [quote=引 用 25 楼 shfodhifdhiauh 的回复:] 这个板块没有版主大神吗?怎么召唤他?
其实就是自己写个比较函数排序就是,函数也简单,假定两个数为a和b那么如果字符串ab>字符串ba,则a>b,反之则小即可,最后按排序顺序就可以了[/quote]你有么有看前面他们举得例子[/quote] 1,10,100,由于110>101,10100〉10010因此1〉10〉100 1,12,122,由于112<121,12122<12212,因此122〉12〉2 [/quote]1,12,121呢?如果位数不同,怎么比较大小?比如12,121?
liuxie99 2014-10-23
  • 打赏
  • 举报
回复
我也觉得应该是a515360208说的 “要把每个数的高位到低位都分开了进行排序,最高位相同的比较次高位,依次类推,”
nice_cxf 2014-10-23
  • 打赏
  • 举报
回复
引用 27 楼 shfodhifdhiauh 的回复:
[quote=引用 26 楼 nice_cxf 的回复:] [quote=引 用 25 楼 shfodhifdhiauh 的回复:] 这个板块没有版主大神吗?怎么召唤他?
其实就是自己写个比较函数排序就是,函数也简单,假定两个数为a和b那么如果字符串ab>字符串ba,则a>b,反之则小即可,最后按排序顺序就可以了[/quote]你有么有看前面他们举得例子[/quote] 1,10,100,由于110>101,10100〉10010因此1〉10〉100 1,12,122,由于112<121,12122<12212,因此122〉12〉2
shfodhifdhiauh 2014-10-23
  • 打赏
  • 举报
回复
引用 26 楼 nice_cxf 的回复:
[quote=引用 25 楼 shfodhifdhiauh 的回复:] 这个板块没有版主大神吗?怎么召唤他?
其实就是自己写个比较函数排序就是,函数也简单,假定两个数为a和b那么如果字符串ab>字符串ba,则a>b,反之则小即可,最后按排序顺序就可以了[/quote]你有么有看前面他们举得例子
nice_cxf 2014-10-23
  • 打赏
  • 举报
回复
引用 25 楼 shfodhifdhiauh 的回复:
这个板块没有版主大神吗?怎么召唤他?
其实就是自己写个比较函数排序就是,函数也简单,假定两个数为a和b那么如果字符串ab>字符串ba,则a>b,反之则小即可,最后按排序顺序就可以了
shfodhifdhiauh 2014-10-23
  • 打赏
  • 举报
回复
这个板块没有版主大神吗?怎么召唤他?
shfodhifdhiauh 2014-10-23
  • 打赏
  • 举报
回复
引用 23 楼 hust_123 的回复:
果断从大到小排序,简单明了
看看前面的回答,,,
shfodhifdhiauh 2014-10-23
  • 打赏
  • 举报
回复
引用 39 楼 zhao4zhong1 的回复:
[quote=引用 38 楼 shfodhifdhiauh 的回复:] [quote=引用 37 楼 boliang319 的回复:] 第一步:遍历这n个正整数,找出位数最多的一个的位数m 第二步:再遍历n个正整数,把不足m位的数以右边补0的方式都补到m位 第三部:对补过位的n个数做排序,比方说快速排序 第四部:从大到小的一次取出n个数,去掉补的零,从左到右排列得到结果。
94 9 4 14 1 94 90 40 14 10 94 9 4 14 1 9 94 4 14 1[/quote]
引用 33 楼 zhao4zhong1 的回复:
//一组正整数,求用这组正整数对应的数字字符串连在一起能组成的最大数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 20
#define MAXL MAXN*(10+1)
char ln[MAXL],*p;
unsigned int uis[MAXN];
int i,n,r;
int compare(const void *a,const void *b) {
    char da[11],db[11],dab[21],dba[21];

    sprintf(da,"%u",*(unsigned int *)a);
    sprintf(db,"%u",*(unsigned int *)b);
    sprintf(dab,"%s%s",da,db);
    sprintf(dba,"%s%s",db,da);
    return strcmp(dba,dab);
}
int main() {
    fgets(ln,MAXL,stdin);
    p=ln;
    i=0;
    while (1) {
        r=sscanf(p,"%u%n",&uis[i],&n);
        if (1==r) {
            i++;
            if (i>=MAXN) {
                printf(">%d number is ignored.\n",MAXN);
                break;
            }
            p+=n;
        } else if (0==r) {
            p++;
        } else break;
    }
    n=i;
    qsort(uis,n,sizeof(unsigned int),compare);
    ln[0]=0;
    for (i=0;i<n;i++) sprintf(ln,"%s%u",ln,uis[i]);
    printf("%s\n",ln);
    return 0;
}
C:\test>test 94 9 4 14 1 9944141 C:\test>test 94 90 40 14 10 9490401410 C:\test>test 94 9 4 14 1 9944141 [/quote]谢谢,写的很好!改变比较大小的规则,ab前后组合谁大放前面最终得到最大数
赵4老师 2014-10-23
  • 打赏
  • 举报
回复
C:\test>test 9 99 98 8 88 89 97 7 1 10 100 12 121 1212 9999897898887121212121110100
赵4老师 2014-10-23
  • 打赏
  • 举报
回复
引用 38 楼 shfodhifdhiauh 的回复:
[quote=引用 37 楼 boliang319 的回复:] 第一步:遍历这n个正整数,找出位数最多的一个的位数m 第二步:再遍历n个正整数,把不足m位的数以右边补0的方式都补到m位 第三部:对补过位的n个数做排序,比方说快速排序 第四部:从大到小的一次取出n个数,去掉补的零,从左到右排列得到结果。
94 9 4 14 1 94 90 40 14 10 94 9 4 14 1 9 94 4 14 1[/quote]
引用 33 楼 zhao4zhong1 的回复:
//一组正整数,求用这组正整数对应的数字字符串连在一起能组成的最大数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 20
#define MAXL MAXN*(10+1)
char ln[MAXL],*p;
unsigned int uis[MAXN];
int i,n,r;
int compare(const void *a,const void *b) {
    char da[11],db[11],dab[21],dba[21];

    sprintf(da,"%u",*(unsigned int *)a);
    sprintf(db,"%u",*(unsigned int *)b);
    sprintf(dab,"%s%s",da,db);
    sprintf(dba,"%s%s",db,da);
    return strcmp(dba,dab);
}
int main() {
    fgets(ln,MAXL,stdin);
    p=ln;
    i=0;
    while (1) {
        r=sscanf(p,"%u%n",&uis[i],&n);
        if (1==r) {
            i++;
            if (i>=MAXN) {
                printf(">%d number is ignored.\n",MAXN);
                break;
            }
            p+=n;
        } else if (0==r) {
            p++;
        } else break;
    }
    n=i;
    qsort(uis,n,sizeof(unsigned int),compare);
    ln[0]=0;
    for (i=0;i<n;i++) sprintf(ln,"%s%u",ln,uis[i]);
    printf("%s\n",ln);
    return 0;
}
C:\test>test 94 9 4 14 1 9944141 C:\test>test 94 90 40 14 10 9490401410 C:\test>test 94 9 4 14 1 9944141
shfodhifdhiauh 2014-10-23
  • 打赏
  • 举报
回复
引用 37 楼 boliang319 的回复:
第一步:遍历这n个正整数,找出位数最多的一个的位数m 第二步:再遍历n个正整数,把不足m位的数以右边补0的方式都补到m位 第三部:对补过位的n个数做排序,比方说快速排序 第四部:从大到小的一次取出n个数,去掉补的零,从左到右排列得到结果。
94 9 4 14 1 94 90 40 14 10 94 9 4 14 1 9 94 4 14 1
boliang319 2014-10-23
  • 打赏
  • 举报
回复
第一步:遍历这n个正整数,找出位数最多的一个的位数m 第二步:再遍历n个正整数,把不足m位的数以右边补0的方式都补到m位 第三部:对补过位的n个数做排序,比方说快速排序 第四部:从大到小的一次取出n个数,去掉补的零,从左到右排列得到结果。
盘子饿了 2014-10-23
  • 打赏
  • 举报
回复
按字典序逆序。
  • 打赏
  • 举报
回复
同#33,把数字当作字符串,字符串比较(字符串比较按照字符位上的大小比较与长度无关)。将大的字符串放在前面,这样得到的整数值就是最大的,得到的整体整数也是最大的。
Morrisss_ 2014-10-23
  • 打赏
  • 举报
回复
每个数都分别拆开,再排序,这就没问题了吧。
赵4老师 2014-10-23
  • 打赏
  • 举报
回复
//一组正整数,求用这组正整数对应的数字字符串连在一起能组成的最大数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXN 20
#define MAXL MAXN*(10+1)
char ln[MAXL],*p;
unsigned int uis[MAXN];
int i,n,r;
int compare(const void *a,const void *b) {
    char da[11],db[11],dab[21],dba[21];

    sprintf(da,"%u",*(unsigned int *)a);
    sprintf(db,"%u",*(unsigned int *)b);
    sprintf(dab,"%s%s",da,db);
    sprintf(dba,"%s%s",db,da);
    return strcmp(dba,dab);
}
int main() {
    fgets(ln,MAXL,stdin);
    p=ln;
    i=0;
    while (1) {
        r=sscanf(p,"%u%n",&uis[i],&n);
        if (1==r) {
            i++;
            if (i>=MAXN) {
                printf(">%d number is ignored.\n",MAXN);
                break;
            }
            p+=n;
        } else if (0==r) {
            p++;
        } else break;
    }
    n=i;
    qsort(uis,n,sizeof(unsigned int),compare);
    ln[0]=0;
    for (i=0;i<n;i++) sprintf(ln,"%s%u",ln,uis[i]);
    printf("%s\n",ln);
    return 0;
}
nice_cxf 2014-10-23
  • 打赏
  • 举报
回复
引用 30 楼 shfodhifdhiauh 的回复:
[quote=引用 28 楼 nice_cxf 的回复:] [quote=引用 27 楼 shfodhifdhiauh 的回复:] [quote=引用 26 楼 nice_cxf 的回复:] [quote=引 用 25 楼 shfodhifdhiauh 的回复:] 这个板块没有版主大神吗?怎么召唤他?
其实就是自己写个比较函数排序就是,函数也简单,假定两个数为a和b那么如果字符串ab>字符串ba,则a>b,反之则小即可,最后按排序顺序就可以了[/quote]你有么有看前面他们举得例子[/quote] 1,10,100,由于110>101,10100〉10010因此1〉10〉100 1,12,122,由于112<121,12122<12212,因此122〉12〉2 [/quote]1,12,121呢?如果位数不同,怎么比较大小?比如12,121?[/quote] 121和12?12112<12121,那么就是12大,两个数只有两种拼法,那个最后结果大,在前边的那个就是大的,如果相同就是相等,比如12=1212,1=11=111等
shfodhifdhiauh 2014-10-22
  • 打赏
  • 举报
回复
引用 4 楼 FancyMouse 的回复:
[quote=引用 3 楼 nice_cxf 的回复:] 按字符串排序,从大到小就ok了
两组数据 1 12 122 ----- 1 10 100[/quote]有没有好方法?
shfodhifdhiauh 2014-10-22
  • 打赏
  • 举报
回复
引用 7 楼 zhao4zhong1 的回复:
全排列+穷举!
来个简单点的,,,
加载更多回复(21)

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧