简单题对我这样的新手也很困难啊
//////////////////////////////////////////////////////////
// ZOJ: 2207
// 简单题:通过枚举出所有ABCDE五个字母组成的120种组合然后
// 判断排名。
//////////////////////////////////////////////////////////
#include "stdio.h"
int ranking(char *duibi, char *input); //用于每个数组里面对数的个数
int main(void)
{
//枚举出所有情况的字母组合
char sort[150][50] = {0};
char szbuf[50] = {"AAAAA"};
char temp[50] = {'A','B','C','D','E'};
int i, j, x, y;
int l = 0;
int z;
int k;
for (i = 0; i < 5; i++)
{
szbuf[0] = temp[i];
for (j = 0; j < 5; j++)
{
szbuf[1] = temp[j];
if (szbuf[0] != szbuf[1])
{
for (x = 0; x < 5; x++)
{
szbuf[2] = temp[x];
if (szbuf[2] != szbuf[1] && szbuf[2] != szbuf[0])
{
for (y = 0; y < 5; y++)
{
szbuf[3] = temp[y];
if (szbuf[3] != szbuf[0] && szbuf[3] != szbuf[1] && szbuf[3] != szbuf[2])
{
for (z = 0; z < 5; z++)
{
szbuf[4] = temp[z];
if (szbuf[4] != szbuf[0] && szbuf[4] != szbuf[1]
&& szbuf[4] != szbuf[2] && szbuf[4] != szbuf[3])
{
for (k = 0; k < 6; k++)
{
sort[l][k] = szbuf[k];
}
l++;
}
}
}
}
}
}
}
}
}
int n;
while(scanf("%d", &n))
{
if (n == 0)
break;
int value[200] = {0};
int count = 0;
int m;
char cal[10] = {0};
int resValue = -1;
int m1;
int index;
while (n-- > 0)
{
scanf("%s", cal);
//循环做判断并把值用一个结果数组保存
for (m = 0; m < 120; m++)
{
value[m] += ranking(sort[m], cal);
}
}
//从120字符串序列的结果值中找出距离值最小的并返回下标
for (m1 = 0; m1 < 120; m1++)
{
if (resValue == -1)
{
resValue = value[m1];
index = m1;
}
else
{
if (resValue > value[m1])
{
resValue = value[m1];
index = m1;
}
}
}
printf("%s is the median ranking with value %d./n", sort[index], resValue);
}
return 0;
}
//用于判断的函数计算出每个字符串对比的结果距离值
int ranking(char *duibi, char *input)
{
int result = 0;
int i;
int dui[10], in[10];
//分别找出A B C D E五个字母的位置
for (i = 0; i < 5; i++)
{
if (duibi[i] == 'A')
dui[0] = i;
else if (duibi[i] == 'B')
dui[1] = i;
else if (duibi[i] == 'C')
dui[2] = i;
else if (duibi[i] == 'D')
dui[3] = i;
else if (duibi[i] == 'E')
dui[4] = i;
}
//参照字符串
for (i = 0; i < 5; i++)
{
if (input[i] == 'A')
in[0] = i;
else if (input[i] == 'B')
in[1] = i;
else if (input[i] == 'C')
in[2] = i;
else if (input[i] == 'D')
in[3] = i;
else if (input[i] == 'E')
in[4] = i;
}
int x, y, z = 0;
int x1, y1, z1 = 0;
int resa[100], resb[100];
int o;
//将五个字母中所有可能值列出
for (x = 0; x < 5; x++)
{
for (y = x + 1; y < 5; y++)
{
resa[z++] = dui[x] - dui[y];
}
}
for (x1 = 0; x1 < 5; x1++)
{
for (y1 = x1 + 1; y1 < 5; y1++)
{
resb[z1++] = in[x1] - in[y1];
}
}
//搜索有产生排名变化的值 发生变化则结果值+1
for (o = 0; o < z; o++)
{
if ((resa[o] > 0 && resb[o] < 0) || (resa[o] < 0 && resb[o] > 0))
{
++result;
}
}
return result;
}