题解 | #字符串排序#

字符串排序

http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584

#include<stdio.h>
#include<string.h>

typedef struct Info
{
    int order;
    char c;
    int value;
}Info;

void exchange(Info *a, Info *b)
{
    Info tmp = *a;
    *a = *b;
    *b = tmp;
}

int main()
{
    char str[1000];
    gets(str);
    int len = strlen(str);
    Info ch[1000];
    for(int i=0; i<len; i++)
    {
        ch[i].order = i;
        ch[i].c = str[i];
        if( isalpha(str[i]) )
            ch[i].value = (str[i]-'A') % ('a'-'A');
        else
            ch[i].value = -2;
    }
    for(int i=0; i<len-1; i++)
    {
        for(int j=i+1; j<len; j++)
        {
            if(ch[i].value != -2 && ch[j].value != -2)
            {
                if(ch[i].value > ch[j].value)
                    exchange(&ch[i], &ch[j]);
                else if(ch[i].value == ch[j].value)
                {
                    if(ch[i].order > ch[j].order)
                        exchange(&ch[i], &ch[j]);
                }
            }
        }
    }
    for(int i=0; i<len; i++)
    {
        printf("%c", ch[i].c);
    }
}
全部评论

相关推荐

xtu大迫杰:偶遇校友,祝校友offer打牌
点赞 评论 收藏
分享
02-25 19:38
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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