求求大佬帮帮我看看
设计n个正整数(n<=20),将它们连成一排,组成一个最大的多位数。程序输入:n,程序输出:n个数连接成的多位数。比如输入这样五个数123 ,2 ,33, 1006, 12
先找出最大的数字的位数为4位数,再将所有的数字后面添加0变成4位数:
1230 2000 3300 1006 1200
然后进行排序:
3300 2000 1230 1200 1006
这样后再奖后面添加的0去掉的序列就是最大的数字:
332123121006
#include <stdio.h> #include <stdlib.h> typedef struct num { unsigned int number; unsigned long longnum; int len; } Num; int getLen(int num) { if (num <= 0) return 0; return 1 + getLen(num / 10); } int main(void) { int n, max = 0; scanf("%d", &n); Num number[20]; for (int i = 0; i < n; ++i) { scanf("%u", &number[i].number); number[i].len = getLen(number[i].number); if (max < number[i].len) max = number[i].len; } for (int x, i = 0; i < n; ++i) { number[i].longnum = number[i].number; x = max - number[i].len; while (x--) number[i].longnum *= 10; } for (int i = 0; i < n - 1; ++i) { Num tmp; for (int j = i + 1; j < n; ++j) { if (number[i].longnum < number[j].longnum) { tmp = number[i]; number[i] = number[j]; number[j] = tmp; } } } for (int i = 0; i < n; ++i) printf("%lu ", number[i].longnum); printf("\n"); unsigned long long maxnum = number[0].number; for (int i = 1; i < n; ++i) { for (int j = 0; j < number[i].len; ++j) { maxnum *= 10; } maxnum += number[i].number; } printf("\n%llu\n", maxnum); system("pause"); return 0; }