1,2,3,4,5可以组成的数

public class NumCompo {

	/**
	 * 1,2,3,4,5可以组成多少个数
	 */
	
	static int count = 5;/*所有元素为1-3的整数*/
	static int total = 0;/*统计可以组成多少个数*/
	
	public static void main(String[] args) {
	
		Method();/*界面而已*/
		
	}

	private static void Method() {
		int n = count;
		getNum(n,0," "); /*打印数值*/
		System.out.println("总共有:"+total);
			
	}
	
	/** 打印数值*/
	private static void getNum(int n,int number,String noInBefore) {
		String noIn = noInBefore;/*用个字符串存储这位数不能选的数,然后传入下次循环*/
		int numb = 0;
		if(n == 1){
			for (int j = 1; j <= count; j++) {
				
				if(restrict35(noIn,j)) continue;/*判断5,3不能连续*/
				if(noIn.indexOf(j+"")>-1 ) continue;
				{
					
					System.out.println(number+j);
					total++;
				}
			}
		}
		if(n > 1) {
			for (int i = 1; i <= count; i++) {
				if(n==3 && i==4) continue;/*限定条件,4不能在第三位*/
				if(restrict35(noIn,i)) continue;/*判断5,3不能连续*/
				if(noIn.indexOf(i+"")>-1 )continue;
				noIn+= i;
				numb = (int) (number + i*Math.pow(10, n-1));
				getNum(n-1,numb,noIn);
				noIn = noInBefore;
			}
		}
	}
	/** 判断5,3不能连续*/
	public static boolean restrict35(String s,int i){
		if(s.indexOf(3+"")==s.length()-1 && i==5){
			return true;
		}
		if(s.indexOf(5+"")==s.length()-1 && i==3){
			return true;
		}
		return false;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值