#include <iostream> using namespace std; int cmp(const void *a, const void *b) { return(*(int *)a - *(int *)b); } int main() { int i, j, k, n, m, count[100], newcount[100]; char **dna; cin >> n >> m; dna = (char **)malloc(sizeof(char *)*m); for(i = 0; i < m; i++) { count[i] = 0; dna[i] = (char *)malloc(sizeof(char)*n); } for(i = 0; i < m; i++) for(j = 0; j < n; j++) cin >> dna[i][j]; for(i = 0; i < m; i++) { for(j = 0; j < n; j++) { for(k = j+1; k < n; k++) { if(dna[i][j]>dna[i][k]) { count[i]++; } } } newcount[i]=count[i]; } qsort(newcount, m, sizeof(int), cmp); for(i = 0; i < m; i++) { for(j = 0; j < m; j++) { if(newcount[i] == count[j]) { for(k = 0; k < n; k++) cout << dna[j][k]; cout<<endl; break; } } } return 0; } 注意二維動態數組的聲明和qsort()的使用 法二: #include <stdio.h> int main() { int n, m, max = 0, min; int i, j, k; char dna[101][51]; int len[101] = {0}; int flag[101] = {0}; scanf("%d %d", &n, &m); for(i = 0; i < m; i++) { scanf("%s", &dna[i]); for(j = 0; j < n; j++) for(k = j+1; k < n; k++) if(dna[i][j] > dna[i][k]) len[i]++; if(len[i] > max) max = len[i]; } for(i = 0; i < m; i++) { min = max + 1; for(j = 0; j < m; j++) { if((len[j] < min) && (flag[j] == 0)) { min = len[j]; k = j; } } flag[k] = 1; printf("%s\n",dna[k]); } return 0; }