习题2-6 排列(permutation)(java版本)

习题2-6 排列(permutation)

用1,2,3,...,9组成3个三位数 abc,def 和 ghi,每个数字恰好使用一次,要求 abc :def:ghi = 1:2:3。

按照“abc def ghi”的格式输出所有的解,每行一个解。


public class permutation {

	public static void main(String[] args) {
		for(int i=111;i<=329;i++){
			Boolean t=true;
			int j=2*i;
			int k=3*i;

			int a[]=new int[9];
			a[0]=i/100;
			a[1]=i/10%10;
			a[2]=i%10;
			
			a[3]=j/100;
			a[4]=j/10%10;
			a[5]=j%10;
			a[6]=k/100;
			a[7]=k/10%10;
			a[8]=k%10;
			
			for(int m=1;m<=9;m++){
				if(a[m-1]==0){
					t=false;
				}
				for(int n=m+1;n<=9;n++){
					if(a[m-1]==a[n-1]){
						t=false;
					}
					
					
				}
			}
			if(t){
				System.out.println(""+a[0]+a[1]+a[2]+" "+a[3]+a[4]+a[5]+" "+a[6]+a[7]+a[8]);
			}
		}
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值