全排列

三羊献瑞


观察下面的加法算式:


       祥 瑞 生 辉
  +   三 羊 献 瑞
-------------------
   三 羊 生 瑞 气


其中,相同的汉字代表相同的数字,不同的汉字代表不同的数字。

请你填写“三羊献瑞”所代表的4位数字(答案唯一),不要填写任何多余内容。

import java.util.Vector;

public class Main {

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		Vector<Integer> source = new Vector<Integer>();
		// 把0-9装到source里面
		for (int i = 0; i < 10; i++) {
			source.add(i);
		}
		Vector<Integer> result = new Vector<Integer>();
		new Main().fullPermutation(source, result);
		System.out.println("耗时:" + (System.currentTimeMillis() - start) + "ms");
	}
	boolean flag = true;
	private void fullPermutation(Vector<Integer> source, Vector<Integer> result) {
		// 还剩2个数
		if (source.size() == 2) {
			// 检测是否满足条件
			if (judge(result)) {
				System.out.println(result.get(4) * 1000 + result.get(5) * 100
						+ result.get(6) * 10 + result.get(1));
				flag=false;
			}
			return;
		}

		for (int i = 0; i < source.size(); i++) {
			if (flag) {
				Vector<Integer> tsource = new Vector<Integer>(source);
				Vector<Integer> tresult = new Vector<Integer>(result);
				tresult.add(tsource.get(i));
				tsource.remove(i);
				fullPermutation(tsource, tresult);
			}else
				return ;
		}
	}

	private boolean judge(Vector<Integer> result) {
		// 去除两个起手为0的情况
		if (result.get(0) == 0 || result.get(4) == 0)
			return false;
		int x = result.get(0) * 1000 + result.get(1) * 100 + result.get(2) * 10
				+ result.get(3);
		int y = result.get(4) * 1000 + result.get(5) * 100 + result.get(6) * 10
				+ result.get(1);
		int z = result.get(4) * 10000 + result.get(5) * 1000 + result.get(2)
				* 100 + result.get(1) * 10 + result.get(7);
		if (x + y == z)
			return true;
		return false;
	}

}


猜算式

看下面的算式:

□□ x □□ = □□ x □□□

它表示:两个两位数相乘等于一个两位数乘以一个三位数。

如果没有限定条件,这样的例子很多。

但目前的限定是:这9个方块,表示1~9的9个数字,不包含0。

该算式中1至9的每个数字出现且只出现一次!

比如:

46 x 79 = 23 x 158

54 x 69 = 27 x 138

54 x 93 = 27 x 186

.....

请编程,输出所有可能的情况!

注意:

左边的两个乘数交换算同一方案,不要重复输出!

不同方案的输出顺序不重要


import java.util.Vector;

public class FullPermutation {

	public static void main(String[] args) {
		long start = System.currentTimeMillis();
		Vector<Character> source = new Vector<Character>();
		for (int i = 0; i < 9; i++) {
			source.add((char) ('1' + i));
		}
		Vector<Character> result = new Vector<Character>();
		permutation(source, result);
		System.out.println(System.currentTimeMillis()-start);
	}

	static boolean isSatisfy(Vector<Character> result) {
		int x = (result.elementAt(0) - '0') * 10 + result.elementAt(1)-'0';
		int y = (result.elementAt(2) - '0') * 10 + result.elementAt(3)-'0';
		if (x < y) {
//			过滤等号前相同的情况
			int z = (result.elementAt(4) - '0') * 10 + result.elementAt(5)-'0';
			int m = (result.elementAt(6) - '0') * 100
					+ (result.elementAt(7) - '0') * 10 + result.elementAt(8)-'0';
			if(x*y==z*m){
				return true;
			}
		}
		return false;
	}

	static void permutation(Vector<Character> source, Vector<Character> result) {
		if (source.size() == 0) {
			if (isSatisfy(result)) {
				System.out.println(result.elementAt(0) + ""
						+ result.elementAt(1) + "x" + result.elementAt(2) + ""
						+ result.elementAt(3) + "=" + result.elementAt(4) + ""
						+ result.elementAt(5) + "x" + result.elementAt(6) + ""
						+ result.elementAt(7) + "" + result.elementAt(8));
			}
			return ;
		}
		for (int i = 0; i < source.size(); i++) {
//			对源的每一个元素做全排列(树状结构)
			Vector<Character> tsource = new Vector<Character>(source);
			Vector<Character> tresult = new Vector<Character>(result);
			tresult.add(tsource.elementAt(i));
			tsource.remove(i);
			permutation(tsource, tresult);
		}
	}
}



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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值