题解 | #字符串排序#

字符串排序

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;
}

全部评论

相关推荐

10-20 11:11
辽宁大学 营销
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务