蓝桥杯 (java)搭积木 暴力破解

搭积木

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

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

下面是两种合格的搭法:

0
1 2
3 4 5
6 7 8 9

0
3 1
7 5 2
9 8 6 4

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

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

暴力破解

package lq;

public class 搭积木 {

public static void main(String[] args) {
	// TODO Auto-generated method stub

int count=0;
for(int x=1;x<=9;x++)
for(int y=1;y<=4;y++)
for(int q=2;q<=7;q++)
for(int w=2;w<=7;w++)
for(int e=2;e<=7;e++)
for(int a=3;a<=9;a++)
for(int b=3;b<=9;b++)
for(int c=3;c<=9;c++)
for(int d=3;d<=9;d++)

	    								if(x!=y&&x!=q&&x!=w&&x!=e&&x!=a&&x!=b&&x!=c&&x!=d&&y!=q&&y!=w&&y!=e&&y!=a&&y!=b&&y!=c&&y!=d&&q!=w&&q!=e&&q!=a&&q!=b&&q!=c&&q!=d&&w!=e&&w!=a&&w!=b&&w!=c&&w!=d&&e!=a&&e!=b&&e!=c&&e!=d&&a!=b&&a!=c&&a!=d&&b!=c&&b!=d&&c!=d) {
	    									
	    									if(q>x&&w>x&&w>y&&e>y&&a>q&&b>q&&b>w&&c>w&&c>e&&d>e) {
	    									System.out.println(0);
	    									System.out.println(x+" "+y);
	    									System.out.println(q+" "+w+" "+e);
	    									System.out.println(a+" "+b+" "+c+" "+d);
	    								count++;
	    								}
	    								}
		
	System.out.println(count);
}

}

猜你喜欢

转载自blog.csdn.net/weixin_43557514/article/details/88525321
今日推荐