- 题目描述:
- 对N个长度最长可达到1000的数进行排序。
- 输入:
- 输入第一行为一个整数N,(1<=N<=100)。
接下来的N行每行有一个数,数的长度范围为1<=len<=1000。
每个数都是一个正数,并且保证不包含前缀零。
- 输出:
- 可能有多组测试数据,对于每组数据,将给出的N个数从小到大进行排序,输出排序后的结果,每个数占一行。
- 样例输入:
-
3 11111111111111111111111111111 2222222222222222222222222222222222 33333333
- 样例输出:
-
33333333 11111111111111111111111111111 2222222222222222222222222222222222
==========================================
#include
#include
#include
using namespace std;
struct BigInt
{
char data[1001]; // 高位在前,低位在后
};
int compare(const void *pA, const void *pB)
{
BigInt *a = (BigInt *)pA;
BigInt *b = (BigInt *)pB;
int len_a = strlen(a->data);
int len_b = strlen(b->data);
if(len_a < len_b)
return -1;
else if(len_a > len_b)
return 1;
else
{
int i;
for(i = 0; i
return a->data[i] - b->data[i];
}
}
return 0;
}
int main()
{
char input[1001];
int N;
while(cin >> N)
{
BigInt *bigInts = new BigInt[N];
int i;
for(i=0; i
strcpy(bigInts[i].data, input);
}
qsort(bigInts, N, sizeof(BigInt), compare);
for(i=0; i