Java实现第八届蓝桥杯纸牌三角形

纸牌三角形

题目描述

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

   A
 9  6

4 8
3 7 5 2

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

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

请你计算并提交该数字。

注意:需要提交的是一个整数,不要提交任何多余内容。
在这里插入图片描述

public class Main {
 
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		int cnt=0;
		for(int a=1;a<=9;a++){
			for(int b=1;b<=9;b++){
				for(int c=1;c<=9;c++){
					for(int d=1;d<=9;d++){
						for(int e=1;e<=9;e++){
							for(int f=1;f<=9;f++){
								for(int g=1;g<=9;g++){
									for(int h=1;h<=9;h++){
										for(int i=1;i<=9;i++){
											if(a!=b && a!=c && a!=d && a!=e && a!=f && a!=g && a!=h && a!=i &&
													b!=c && b!=d && b!=e && b!=f && b!=g && b!=h && b!=i &&
													c!=d && c!=e && c!=f && c!=g && c!=h && c!=i &&
													d!=e && d!=f && d!=g && d!=h && d!=i &&
													e!=f && e!=g && e!=h && e!=i &&
													f!=g && f!=h && f!=i &&
													g!=h && g!=i && 
													h!=i){
												if((a+b+d+f)==(a+c+e+i) && (a+b+d+f)==(f+g+h+i) && (a+c+e+i)==(f+g+h+i)){
													cnt++;
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		System.out.println(cnt/3/2);	//旋转3种,镜像2种
	}
 
}

PS: 我的旋转有三种情况,镜像有两种,我使用我总次数除6,即可(ง •_•)ง

package 第五次模拟;

public class Demo3纸牌三角形 {
	static	int [] num = new int [10];
	static int count=0;
	static	boolean [] bool = new boolean [10];
		public static void main(String[] args) {
			dfs(1);
			System.out.println(count/3/2);
		}
		public static void dfs(int step){
			if(step==10){
				
			if(num[1]+num[2]+num[4]+num[6]==num[1]+num[3]+num[5]+num[9] &&
					num[1]+num[2]+num[4]+num[6]==num[6]+num[7]+num[9]+num[8]	
						){
				count++;
			}
				
				return;
			}
			for (int i = 1; i <10; i++) {
				if (!bool[i]) {
					bool[i]=true;
					num[step]=i;
					dfs(step+1);
					bool[i]=false;
					
					
				}
			}
		}


}

原文地址:https://www.cnblogs.com/a1439775520/p/12947609.html