zoukankan      html  css  js  c++  java
  • 纸牌三角形

    /*A,2,3,4,5,6,7,8,9 共9张纸牌排成一个正三角形(A按1计算)。要求每个边的和相等。 
    下图就是一种排法(如有对齐问题,参看p1.png)。
    
        A
       9 6
      4   8
     3 7 5 2
    1
    2
    3
    4
    5
    这样的排法可能会有很多。
    
    如果考虑旋转、镜像后相同的算同一种,一共有多少种不同的排法呢?
    
    请你计算并提交该数字。
    
    注意:需要提交的是一个整数,不要提交任何多余内容。*/
    package test;
    
    public class 纸牌三角型 {
        static int count=0;
        static void swap(int[] s,int a,int b){
            int temp=s[a];
            s[a]=s[b];
            s[b]=temp;
        }
        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[1]==b[2]){
                return true;
            }
            return false;
        }
        public static void gather(int[] a,int length){
            if(length>=a.length){
                if(check(a)){
                    count++;
                    return;
                }
            }
            for(int i=length;i<a.length;i++){
                swap(a,i,length);
                gather(a,length+1);//递归调用,将后面的数先进行排列
                swap(a,length,i);//回溯
            }
        }
        public static void main(String arg[]){
             int[] str={1,2,3,4,5,6,7,8,9};
               gather(str,0);
               System.out.print(count/6);//去除旋转,镜像
          }
    }
  • 相关阅读:
    Path Sum
    Intersection of Two Linked Lists (求两个单链表的相交结点)
    Nginx入门资料
    赛马问题
    翻转单词顺序 VS 左旋转字符串
    重建二叉树
    Fibonacci相关问题
    Find Minimum in Rotated Sorted Array(旋转数组的最小数字)
    常用查找算法总结
    Contains Duplicate
  • 原文地址:https://www.cnblogs.com/ljs-666/p/8595682.html
Copyright © 2011-2022 走看看