Sicily 1006. Team Rankings 全排列

        水题,把所有排列全列出来,找出最小的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;

		}

	}

}

你可能感兴趣的:(rank)