纸牌三角形

标题:纸牌三角形

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

      A
     9 6
    4   8
   3 7 5 2


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


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


请你计算并提交该数字。


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

【解法一】

暴力循环

package com.sise.test;

public class Test01 {
	
	
	public static void main(String[] args){
		
		int count=0;
		
		for(int a=1;a<10;a++){
			for(int b=1;b<10;b++){
				if(a!=b){
					for(int c=1;c<10;c++){
						if(a!=c&&b!=c){
							for(int d=1;d<10;d++){
								if(a!=d&&b!=d&&c!=d){
									for(int e=1;e<10;e++){
										if(a!=e&&b!=e&&c!=e&&d!=e){
											for(int f=1;f<10;f++){
												if(a!=f&&b!=f&&c!=f&&d!=f&&e!=f){
													for(int g=1;g<10;g++){
														if(a!=g&&b!=g&&c!=g&&d!=g&&e!=g&&f!=g){
															for(int h=1;h<10;h++){
																if(a!=h&&b!=h&&c!=h&&d!=h&&e!=h&&f!=h&&g!=h){
																	for(int i=1;i<10;i++){
																		if(a!=i&&b!=i&&c!=i&&d!=i&&e!=i&&f!=i&&g!=i&&h!=i){
																			if((a+b+c+d==d+e+f+g)&&(a+b+c+d==g+h+i+a)){
																				count++;
																			}
																		}
																	}
																}
															}
														}
													}
												}
											}
										}
									}
								}
							}
						}
					}
				}
			}
		}
		System.out.println(count/6);
	}
}

【解法二】


全排列

package com.sise.test;

public class Test01 {
	
	public static int count=0;
	
	public static void main(String[] args){
		
		int[] str={1,2,3,4,5,6,7,8,9};
		
		arrange(str,0);
			
		System.out.println(count/6);
	}
	
	public static void arrange(int[] str,int st){
		if(st>=str.length){
			if(check(str)){
				count++;
				return;
			}
		}else{
			for(int i=st;i<str.length;i++){
				swap(str,st,i);
				arrange(str,st+1);
				swap(str,i,st);
			}		
		}
	}
	
	public static boolean check(int[] a){
		int[] b=new int[3];
		b[0]=a[0]+a[1]+a[2]+a[3];
		b[1]=a[3]+a[4]+a[5]+a[6];
		b[2]=a[6]+a[7]+a[8]+a[0];
		if(b[0]==b[1]&&b[0]==b[2]){
			return true;
		}
		return false;
	}
	
	public static void swap(int[] str,int i,int j){
		int temp=str[i];
		str[i]=str[j];
		str[j]=temp;
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值