• 7.搭积木


    声明

    可能本文章会有错误,希望各位读者看到后,记得回复留言,提醒我,以免误人子弟。本人菜鸡,还望各位大佬手下留情。

    题目

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

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

    下面是两种合格的搭法:

    0
    1 2
    3 4 5
    6 7 8 9

    0
    3 1
    7 5 2
    9 8 6 4

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

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


    分析

    本题也用到了全排列,和上一题差不多,只是条件不同而已。
    博主采用的是最简单的方式,思路有限,/滑稽一下。


    代码

    public class g {
    	static int count = 0;
    	public static void da(int[] list) {
    		if(list[0] < list[1] && list[0] < list[2]) {
    			if((list[1] <list[3] && list[1]<list[4]) && (list[2] <list[4] && list[2]<list[5])) {
    				if((list[3] <list[6] && list[3]<list[7]) && (list[4] <list[7] && list[4]<list[8]) && (list[5] <list[8] && list[5]<list[9])) {
    					count++;
    				}
    			}
    		}
    	}
    	
    	public static void swap(int[] list,int a,int b) {
    		int temp = list[a];
    		list[a] = list[b];
    		list[b] = temp;
    	}
    	
    	public static void fun(int[] list,int begin,int length) {
    		if(begin == length) {
    			da(list);
    		}else {
    			for(int i=begin;i<=length;i++) {
    				swap(list, begin, i);
    				fun(list, begin+1, length);
    				swap(list, begin, i);
    			}
    		}
    	}
    	
    	public static void main(String[] args) {
    		int[] list = {0,1,2,3,4,5,6,7,8,9};
    		fun(list,0,list.length-1);
    		System.out.println(count);
    	}
    }
    
    
  • 相关阅读:
    C# 访问USB(HID)设备
    IPad C盘中backup文件夹占用太多空间
    fastboot
    adb get android's ip
    Reading MMS
    Bitcoin
    内存问题导致编译不过!
    串口LOG 单编kernel
    Source Insight 格式化
    framework层的空指针会让系统重启
  • 原文地址:https://www.cnblogs.com/drinkoo/p/8744768.html
Copyright © 2020-2023  润新知