View Code of Problem 126

#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

typedef struct DNA{
	char str[51];
	int num; //记录逆序数 
} data;

bool cmp(data a, data b){
	return  a.num < b.num; //从小到大 
} 

int main(){
	int n, m; //n是长度,m是个数 
	while(scanf("%d %d", &n, &m) != EOF) {
			data arr[m]; //一共有m个结构体 
			for(int i=0; i<m; i++){ 
				scanf("%s", arr[i].str); //一边输入一遍比较。输入一个,比较一个 
				arr[i].num = 0;
				
				for(int j=0; j<n; j++){ //对输入进来的str计算其逆序数 
					for(int k=j+1; k<n; k++){
						if(arr[i].str[j] > arr[i].str[k]){
							arr[i].num++; //记录逆序数 
						}
					}
				}	
			}
			
			sort(arr, arr+m, cmp);  //我们开的arr[m],所以这里是arr+m 
			for(int i=0; i<m; i++){
				printf("%s\n", arr[i].str);
			} 
	}
	return 0;
}

Double click to view unformatted code.


Back to problem 126