搭积木.java

package A类有价值的回顾的;

public class 搭积木 {

    public static int n = 0;
    public static boolean[] b = new boolean[10];
    public static int[] c = new int[10];


    public static boolean f2(int a,int i){//有时候如果他们少的话可以把他们枚举出来就可以了

        if(a == 0) return true;
         if(a == 1&&i>c[0]) return true; 
         if(a == 3&&i>c[1]) return true;
         if(a == 6&&i>c[3]) return true;

         if(a == 2&&i>c[0]) return true;
         if(a == 5&&i>c[2]) return true;
         if(a == 9&&i>c[5]) return true;

         if(a == 4&&i>c[1]&&i>c[2]) return true;
         if(a == 7&&i>c[3]&&i>c[4]) return true;
         if(a == 8&&i>c[4]&&i>c[5]) return true;

         return false;
    }

    public static void  f(int a){

        if(a == 10){
            n++;
            return;
        }
        for(int i=0;i<=9;i++){

            if(f2(a,i) == true && b[i] == false){
                c[a] = i;
                b[i] = true;
                f(a+1);
                b[i] = false;
            }


        }
        return;//如果for循环中一个都没有,那就返回到上一层
    }

    public static void main(String[] args){

        f(0);
        System.out.println(n);

    }

}
/*
 * 搭积木

小明最近喜欢搭数字积木,
一共有10块积木,每个积木上有一个数字,0~9。

搭积木规则:
每个积木放到其它两个积木的上面,并且一定比下面的两个积木数字小。
最后搭成4层的金字塔形,必须用完所有的积木。

下面是两种合格的搭法:

   0
  1 2
 3 4 5
6 7 8 9

   0
  3 1
 7 5 2
9 8 6 4    

请你计算这样的搭法一共有多少种?

请填表示总数目的数字。
注意:你提交的应该是一个整数,不要填写任何多余的内容或说明性文字。




 */




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值