P1012 [NOIP1998 提高组] 拼数
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LENGTH 11
int minn(int a, int b) {
return a < b ? a : b;
}
int compare_number(const void* a, const void* b) {//特殊情况:135和13,32和321
int str_a = strlen((char*)a), str_b = strlen((char*)b);
int str_min = minn(str_a, str_b);
int answer = strncmp((char*)b, (char*)a, str_min);//比较两个字符串的公共长度部分
if (answer != 0 || str_a == str_b)//如果已经能判断出结果,就返回
return answer;
else if (str_a > str_b)//如果字符串a的长度更长,就比较a的剩余部分和字符串b
return compare_number(a + str_min, b);
else // if (str_b > str_a)
return compare_number(a, b + str_min);
}
int main() {
int n;
scanf("%d", &n);
char number[n][LENGTH];
for (int i = 0; i < n; i++)
scanf("%s", &number[i]);//输入
qsort(number, n, sizeof(number[0]), compare_number);//排序
for (int i = 0; i < n; i++)
printf("%s", number[i]);//输出
system("pause");
return 0;
}
标签:compare,return,拼数,int,number,P1012,char,str,NOIP1998
From: https://www.cnblogs.com/fjnhyzCYL/p/17059102.html