虽然过了,但是是试了几种方法才过的,我对题目的median有点不解,median不是中值吗? #include <iostream> #include <cstring> #include <string> #include <cmath> using namespace std; string s[120]; string tmp = "ABCDE"; bool isVis[5]; string ini = "ABCDE"; string data[101]; int c; void _set(int cur) { if(cur == 5) { s[c++] = tmp; return; } for(int i = 0; i < 5; i++) { if(!isVis[i]) { isVis[i] = true; tmp[cur] = ini[i]; _set(cur + 1); isVis[i] = false; } } } int main() { c = 0; //freopen("C:/Users/Alfred/Desktop/1.txt", "r", stdin); //freopen("C:/Users/Alfred/Desktop/2.txt", "w", stdout); memset(isVis, false, sizeof(isVis)); _set(0); int n; while(cin >> n && n != 0) { int k = 0; int _min = 100000000; for(int i = 0; i < n; i++) cin >> data[i]; for(int l = 0; l < 120; l++) { int count = 0; int pos; for(int i = 0; i < n; i++) { tmp = data[i]; for(int j = 0; j < 5; j++) { pos = data[i].find(s[l][j]); count += pos; data[i].erase(pos, 1); } data[i] = tmp; } if(_min > count) { _min = count; k = l; } } cout << s[k] << " is the median ranking with value " << _min << "."<< endl; } return 0; }