`
lisongaxy
  • 浏览: 4535 次
  • 性别: Icon_minigender_1
  • 来自: 福州
文章分类
社区版块
存档分类
最新评论

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

J# 
阅读更多
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;
	}
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics