标题:纸牌三角形
A,2,3,4,5,6,7,8,9 共9张纸牌排成一个正三角形(A按1计算)。要求每个边的和相等。 下图就是一种排法(如有对齐问题,参看p1.png)。
这样的排法可能会有很多。
如果考虑旋转、镜像后相同的算同一种,一共有多少种不同的排法呢?
请你计算并提交该数字。
注意:需要提交的是一个整数,不要提交任何多余内容。
package com.gxwz.test;
public class Main {
static int A[] = {1,2,3,4,5,6,7,8,9};
static int N = 9*8*7*6*5*4*3*2*1; //9位数的全排列一共有 9的阶层 次
static int B[][] = new int [N][A.length]; //二维数组存储全排列
static int count = 0; //全排列次数
static int sum = 0; //结果
public static void main(String[] args) {
f(0,A.length);
dfs();
// outB();
// System.out.println(count);
System.out.println(sum/6);
}
//交换函数
public static void swap(int i,int j) {
int temp = A[i];
A[i] = A[j];
A[j] = temp;
}
//输出数组A
public static void outA() {
for (int i : A) {
System.out.print(i+" ");
}System.out.println();
}
//输出数组B
public static void outB() {
for (int[] is : B) {
for (int i : is) {
System.out.print(i+" ");
}System.out.println();
}
}
//把数组A复制给数组B
public static void copy() {
for(int i=0;i<A.length;i++) {
B[count][i] = A[i];
}
}
//递归,全排列
public static void f(int start,int end) {
if(start == end - 1) {
// outA();
copy();
// outB();
count++;
}else {
for(int i=start;i<end;i++) {
swap(i,start);
f(start+1,end);
swap(i,start);
}
}
}
//校验
public static void dfs() {
for(int i=0;i<B.length;i++) {
if(B[i][0]+B[i][1]+B[i][2]+B[i][3] == B[i][3]+B[i][4]+B[i][5]+B[i][6] &&
B[i][3]+B[i][4]+B[i][5]+B[i][6] == B[i][0]+B[i][6]+B[i][7]+B[i][8]) {
sum++;
}
}
}
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有