纸牌三角形-初识next_permutation

问题描述
A,2,3,4,5,6,7,8,9 共9张纸牌排成一个正三角形(A按1计算)。要求每个边的和相等。
下图就是一种排法。

  A
 9 6
4   8

3 7 5 2
Copy
(如有对齐问题,参看p1.png)

这样的排法可能会有很多。

如果考虑旋转、镜像后相同的算同一种,一共有多少种不同的排法呢?

请你计算并提交该数字。

注意:需要提交的是一个整数,不要提交任何多余内容。

输入
没有输入

输出
一个整数。

提示
把答案放在输出语句中输出,例如C/C++语言可以用printf或cout。
思路
next_permutation的第一次交手

#include <bits/stdc++.h>
using namespace std;
int ans=0;

void process(int *b){
    
    //给三边的数编号 ,固定位置旋转3种,对称三种,6个重复
	int x1=b[0]+b[1]+b[3]+b[5];
	int x2=b[0]+b[2]+b[4]+b[8];
	int x3=b[5]+b[6]+b[7]+b[8];
	if(x1==x2&&x2==x3)
	ans++;
	return ;
}

int main() {
    
    
	int n=9;
	int a[]={
    
    1,2,3,4,5,6,7,8,9};
	do{
    
    
		process(a);
	}while(next_permutation(a,a+n));
	printf("%d\n",ans/6);
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43959743/article/details/113091658