本题并不难,但写此题时,我学到了通过使用稳定的sort 来对结构体进行排序。 以下是代码: #include<stdio.h> #include<algorithm> using namespace std; struct str { char s[52]; int num; }a[101]; //注意返回值是bool类型,必须判断情况,而不能直接返回int型; bool cmp(struct str &a,struct str &b) { if(a.num < b.num) return 1; else return 0; } int main() { int n,m,i,j,k; while(scanf("%d%d",&m,&n)!=EOF) { for(i=0;i<n;i++) scanf("%s",&a[i].s); for(i=0;i<n;i++) { a[i].num=0; for(j=0;j<m-1;j++) { for(k=j+1;k<m;k++) if(a[i].s[j]>a[i].s[k]) a[i].num++; } } //用sort对结构体进行排序; sort(a,a+n,cmp); for(i=0;i<n;i++) printf("%s/n",a[i].s); } return 0; }