水题,把所有排列全列出来,找出最小的rank即可

#include<iostream>
#include <string>
#include <memory.h>
using namespace std;

string hold = "ABCDE";
string result;
string a[101];
int total;
void all_permutation(int current);
int calculate();
int find(char);

int mini, n;
bool used[5];

int main()
{
	while (cin >> n && n)
	{
		memset(used, false, 5*sizeof(bool));

		for (int i = 0; i < n; i++)
			cin >> a[i];
		mini = calculate();
		result = hold;
		all_permutation(0);
		cout << result << " is the median ranking with value " << mini << "." << endl;
	}
	return 0;
}

void all_permutation(int current)
{
	if (current >= 5)
	{
		total = calculate();
		if (total < mini)
		{
			mini = total;
			result = hold;
		}
	}
	else
	{
		for (int i = 0; i < 5; i++)
			if (!used[i])
			{
				used[i] = true;
				hold[current] = i+'A';
				all_permutation(current+1);
				used[i] = false;
			}
	}
}

int calculate()
{
	int result = 0;
	for (int i = 0; i < n; i++)
	{
		for (int j = 0; j < 4; j++)
		{
			for (int k = j+1; k < 5; k++)
			{
				if (find(a[i][j]) > find(a[i][k]))
					result++;
			}
		}
	}
	return result;
}

int find(char x)
{
	int index;

	for (int i = 0; i < 5; i++)
	{
		if (hold[i] == x)
		{
			index = i;
			return index;
		}
	}
}

相关文章:

  • 2022-12-23
  • 2021-05-28
  • 2022-01-06
  • 2018-03-10
猜你喜欢
  • 2022-12-23
  • 2021-06-17
  • 2022-12-23
  • 2022-02-02
  • 2021-06-22
  • 2021-07-12
  • 2022-12-23
相关资源
相似解决方案