此程序耗费我尽3个小时之久,原因是做题前的规划没做好,一直没有想到整体排序的好办法,最后还是用了注意匹配的方法才解决了问题,我不知道为什么用冒泡不行,第一个字符串总是乱码。我觉得整体思路还是比较清晰的,只是方法可能有点傻,效率还行。
C 编译器 : 172K 0MS
#include <stdio.h>
#include <string.h>

typedef struct DNA


{
char str[50]; // 存储字符串
int count[2]; // [0] [1]都存放串的逆序数
}DNA; // [1]中作为参考,用来和排序后的[0]匹配

int main()


{
int i=0,j,k=0,n,m,temp;
DNA or[100];
scanf("%d%d",&n,&m);
while (k<m) //获得数据并求各自逆序数

{
scanf("%s",&or[k].str);
or[k].count[0]=0; // 此步不能忘
for (i=0; i<n; i++)
for (j=i+1; j<n; j++)
if (or[k].str[i] > or[k].str[j])
or[k].count[0]++;
k++;
}
for (i=0; i<m; i++)
or[i].count[1]=or[i].count[0]; // 原逆序数存放顺序

for (i=1; i<m; i++) // 对于各组串的逆序数进行排序,count[0]内容已打乱

{
k=i-1;
for (j=i; j<m; j++)
if (or[j].count[0] < or[k].count[0])
k=j;
temp=or[i-1].count[0];
or[i-1].count[0]=or[k].count[0];
or[k].count[0]=temp;
} // 这是典型的选择排序,只是对[0]单元的处理,稳定与否没关系

for (i=0; i<m; i++)
for (j=0; j<m; j++)
if (or[i].count[0] == or[j].count[1]) // [0] 和 [1] 中逐一相比较

{
or[j].count[1]=-1; // 此步是相等时顺序不变的保证,相当于做了访问标记!
printf("%s\n",or[j].str);
}

return 0;
}