蓝桥杯试题——随意组合

蓝桥杯试题——随意组合

答案:24
#include
#include
int num[9] = { 0,1,2,3,4,5,6,7,8 };
int ans;
using namespace std;

int accord()
{
	int a1, a2, a3, a4, b1, b2, b3, b4, sum1, sum2;
	a1 = num[1] * 10 + num[2];
	a2 = num[3] * 10 + num[4];
	a3 = num[5] * 10 + num[6];
	a4 = num[7] * 10 + num[8];
	b1 = num[2] * 10 + num[1];
	b2 = num[4] * 10 + num[3];
	b3 = num[6] * 10 + num[5];
	b4 = num[8] * 10 + num[7];
	a1 *= a1, a2 *= a2, a3 *= a3, a4 *= a4, b1 *= b1, b2 *= b2, b3 *= b3, b4 *= b4;
	sum1 = a1 + a2 + a3 + a4;
	sum2 = b1 + b2 + b3 + b4;
	if (sum1 == sum2) return 1;
	else return 0;
}
void swap(int &n, int &m)
{
	int temp;
	temp = n;
	n = m;
	m = temp;
}
void f(int n, int len)
{
	if (n == len) {
		if (accord())
			ans++;
		return;
	}
	for (int i = n; i <= 8; i++) {
		swap(num[n], num[i]);
		f(n + 1, len);
		swap(num[n], num[i]);
	}
}

int main() {
	ans = 0;
	f(1, 9);
	ans = ans / (4 * 3 * 2 * 1);//除以四的全排
	ans = ans / 2;//除以一组中两个数先后重复的情况
	cout << ans;
	return 0;
}

你可能感兴趣的:(蓝桥杯)