#大整数排序#用C的qsort(大佬们写的C++,菜鸡补充)
大整数排序
https://www.nowcoder.com/practice/b744af632ac4499aa485d7bb048bb0aa
#include <stdio.h> #include <string.h> #include <math.h> #include <stdlib.h> typedef struct in_num { char num[1001]; int length; }in; int cmp(const void* a,const void* b) { in c = *(in*) a; in d = *(in*) b; if(c.length == d.length) { for(int i=0;i<c.length;i++) { if(c.num[i] != d.num[i]) { return c.num[i] - d.num[i]; } } return c.num - d.num; } else { return c.length - d.length; //长度升序 } } int main() { //大整数用字符串存储,先比长度再按位置比较 int n; while(scanf("%d",&n) != EOF) { in number[n]; for(int i=0;i<n;i++) { scanf("%s",number[i].num); number[i].length = strlen(number[i].num); } qsort(number,n,sizeof(in),cmp); for(int i=0;i<n;i++) { printf("%s\n",number[i].num); } } return 0; }