STL里的next_permutation和strchr的配合使用,很方便
#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
int main()
{
int n;
char input[100][6];
while (scanf("%d",&n)!=EOF)
{
if (n==0)
break;
char team[6]={'A','B','C','D','E'};
for (int i=0;i<n;i++)
{
scanf("%s",input[i]);
}
int count,min=9999;
char output[6];
do
{
count=0;
for (int i=0;i<n;i++)
{
for (int j=0;j<4;j++)
{
for (int k=j+1;k<5;k++)
{
if (strchr(input[i],team[j])>strchr(input[i],team[k]))
{
count++;
}
}
}
}
if (count<min)
{
min=count;
memcpy(output,team,6);
}
} while (next_permutation(team,team+5));
printf("%s is the median ranking with value %d.\n",output,min);
}
}