题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5af18ba2eb45443aa91a11e848aa6723
#include <stdio.h>
#include <malloc.h>
#include <string.h>
int judge(char *s1,char *s2)
{
int len1=strlen(s1);
int len2=strlen(s2);
for(int i=0;i<len1&&i<len2;i++)
{
if(s1[i]<s2[i]) return 1;
if(s1[i]>s2[i]) return 2;
}
if(len1<len2) return 1;
if(len1>len2) return 2;
return 0;
}
int main() {
int n;
scanf("%d", &n);
char str[n][101];
for (int i = 0; i < n; i++)
scanf("%s", str[i]);
for(int i=0;i<n;i++)
{
for(int j=i+1;j<n;j++)
{
if(judge(str[i],str[j])==2)
{
char *flag=malloc(sizeof(char)*100);
strcpy(flag,str[i]);
strcpy(str[i],str[j]);
strcpy(str[j],flag);
}
}
}
for(int i=0;i<n;i++)
printf("%s\n",str[i]);
return 0;
}
上海得物信息集团有限公司公司福利 1166人发布