题解 | #字符串排序#
字符串排序
http://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
#include <stdio.h>
#include <string.h>
typedef struct chInfo
{
int order;
char c;
int value;
} chInfo;
//其实可以取出来原位置记为空 再排序后填入
//要点 多组输入注意每组输出后printf \n
int main()
{
char str[1000];
int len;
while (gets(str) != NULL)
{
len = strlen(str);
chInfo cch[len];
for (int i = 0; i < len; i++)
{
cch[i].c = str[i];
cch[i].order = i;
if(str[i]>='a'&& str[i]<='z' || str[i]>='A' && str[i]<='Z')
cch[i].value = (str[i] - 'A') % ('a' - 'A');
else
cch[i].value = -2;
}
//选择排序
chInfo max;
int maxnum;
for (int j = len - 1; j >= 0; j--)
{
if (cch[j].value != -2)
{
max.value = -1;
for (int i = 0; i <= j; i++)
{
if (cch[i].value > max.value)
max = cch[i], maxnum = i;
else if (cch[i].value == max.value)
{
if (cch[i].order > max.order)
max = cch[i], maxnum = i;
}
}
chInfo temp;
temp = cch[j];
cch[j] = cch[maxnum];
cch[maxnum] = temp;
}
}
for (int i = 0; i < len; i++)
printf("%c", cch[i].c);
printf("\n");
}
return 0;
}