您的位置:首页 > 其它

纸牌三角形

2018-03-30 14:53 176 查看
标题:纸牌三角形

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;
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: