论坛首页 Java企业应用论坛

也写了个火柴移动问题,用了enum

浏览 1670 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2010-09-16   最后修改:2010-09-16

问题描述请看原帖:http://www.iteye.com/topic/399628

 

直接给出代码

import java.util.Collection;
import java.util.LinkedList;

/**
 * @Filename: NumberCode.java
 * @Description: 八段码

 * @author: luw.
 * @version: 1.0
 * @Create at: 2010-9-15 上午09:13:28
 */
public enum NumberCode {
	/** 0 */
	ZERO(0, 0x3F),
	/** 1 */
	ONE(1, 0x06),
	/** 2 */
	TWO(2, 0x5B),
	/** 3 */
	THREE(3, 0x4F),
	/** 4 */
	FOUR(4, 0x66),
	/** 5 */
	FIVE(5, 0x6D),
	/** 6 */
	SIX(6, 0x7D),
	/** 7 */
	SEVEN(7, 0x07),
	/** 8 */
	EIGHT(8, 0x7F),
	/** 9 */
	NINE(9, 0x6F);
	// A(10, 0x77),
	// B(11, 0x7C),
	// C(12, 0x39),
	// D(13, 0x5E),
	// E(14, 0x79),
	// F(15, 0x71);

	private int value;
	private byte code;

	private NumberCode(int value, int code) {
		this(value, (byte) code);
	}

	private NumberCode(int value, byte code) {
		this.value = value;
		this.code = code;
	}

	public int intValue() {
		return this.value;
	}

	public byte codeValue() {
		return this.code;
	}

	/**
	 * 自身移动N位得到的数字
	 * 
	 * @param change
	 *            改变的位数
	 * @return
	 */
	public Collection<NumberCode> canConverts(int change) {
		Collection<NumberCode> numberCodes = new LinkedList<NumberCode>();
		int bitNum = countBit(this.code);
		for (NumberCode nc : values()) {
			if (this == nc) {
				continue;
			}
			if (countBit(nc.code) == bitNum && countBit(nc.code | this.code) - bitNum == change) {
				numberCodes.add(nc);
			}
		}
		return numberCodes;
	}

	/**
	 * 增加N位得到数字
	 * 
	 * @param change
	 *            增加的位数
	 * @return
	 */
	public Collection<NumberCode> addConverts(int change) {
		Collection<NumberCode> numberCodes = new LinkedList<NumberCode>();
		int bitNum = countBit(this.code);
		for (NumberCode nc : values()) {
			if (this == nc) {
				continue;
			}
			if (countBit(nc.code) - bitNum == change && countBit(nc.code | this.code) - bitNum == change) {
				numberCodes.add(nc);
			}
		}
		return numberCodes;
	}

	/**
	 * 减少N位得到的数字
	 * 
	 * @param change
	 *            减少的位数
	 * @return
	 */
	public Collection<NumberCode> subConverts(int change) {
		Collection<NumberCode> numberCodes = new LinkedList<NumberCode>();
		int bitNum = countBit(this.code);
		for (NumberCode nc : values()) {
			if (this == nc) {
				continue;
			}
			final int num = countBit(nc.code);
			if (bitNum - num == change && countBit(nc.code | this.code) - num == change) {
				numberCodes.add(nc);
			}
		}
		return numberCodes;
	}

	@Override
	public String toString() {
		return String.valueOf(value);
	}

	/**
	 * 计算二进制数种1的个数
	 * 
	 * @param code
	 * @return
	 */
	public static int countBit(int code) {
		int iCode = code;
		iCode = (iCode & 0x55555555) + (iCode >>> 1 & 0x55555555); // 得到每2位二进制数中1的个数
		iCode = (iCode & 0x33333333) + (iCode >>> 2 & 0x33333333); // 得到每4位二进制数中1的个数
		iCode = (iCode & 0x0F0F0F0F) + (iCode >>> 4 & 0x0F0F0F0F); // 得到每8位二进制数中1的个数
		iCode = (iCode & 0x00FF00FF) + (iCode >>> 8 & 0x00FF00FF); // 得到每16位二进制数中1的个数
		iCode = (iCode & 0x0000FFFF) + (iCode >>> 16 & 0x0000FFFF);// 以此类推可以得到N位二进制数中1的个数

		return iCode;
	}

	public static NumberCode valueOf(char val) {
		return valueOf(val - 48);
	}

	public static NumberCode valueOf(int val) {
		switch (val) {
		case 0:
			return ZERO;
		case 1:
			return ONE;
		case 2:
			return TWO;
		case 3:
			return THREE;
		case 4:
			return FOUR;
		case 5:
			return FIVE;
		case 6:
			return SIX;
		case 7:
			return SEVEN;
		case 8:
			return EIGHT;
		case 9:
			return NINE;

		default:
			throw new AssertionError("Unknown val: " + val);
		}
	}
}

 

 * @Filename: Operator.java
 * @Description: 运算符
 * 
 * @author: luw.
 * @version: 1.0
 * @Create at: 2010-9-15 上午09:30:10
 */
public enum Operator {
	/** 加 */
	ADD('+') {
		@Override
		public double calculate(double num1, double num2) {
			return num1 + num2;
		}

		@Override
		public double calcNum1(double result, double num2) {
			return SUBTRACT.calculate(result, num2);
		}

		@Override
		public double calcNum2(double result, double num1) {
			return SUBTRACT.calculate(result, num1);
		}
	},
	/** 减 */
	SUBTRACT('-') {
		@Override
		public double calculate(double num1, double num2) {
			return num1 - num2;
		}

		@Override
		public double calcNum1(double result, double num2) {
			return ADD.calculate(result, num2);
		}

		@Override
		public double calcNum2(double result, double num1) {
			return calculate(num1, result);
		}

	},
	/** 乘 */
	MULTIPLY('*') {
		@Override
		public double calculate(double num1, double num2) {
			return num1 * num2;
		}

		@Override
		public double calcNum1(double result, double num2) {
			return DIVIDE.calculate(result, num2);
		}

		@Override
		public double calcNum2(double result, double num1) {
			return DIVIDE.calculate(result, num1);
		}

	},
	/** 除 */
	DIVIDE('/') {
		@Override
		public double calculate(double num1, double num2) {
			return num1 / num2;
		}

		@Override
		public double calcNum1(double result, double num2) {
			return MULTIPLY.calculate(result, num2);
		}

		@Override
		public double calcNum2(double result, double num1) {
			return calculate(num1, result);
		}
	};

	private final char op;

	private Operator(char op) {
		this.op = op;
	}

	@Override
	public String toString() {
		return " " + op + " ";
	}

	/**
	 * 计算两个数之间进行(加/减/乘/除)后的结果
	 * 
	 * @param num1
	 *            第一个数(操作符左边的那个数)
	 * @param num2
	 *            第二个数(操作符右边的那个数)
	 * @return
	 */
	public abstract double calculate(double num1, double num2);

	/**
	 * 根据结果和第二个操作数得到第一个操作数
	 * 
	 * @param result
	 *            结果
	 * @param num2
	 *            第二个操作数
	 * @return
	 */
	public abstract double calcNum1(double result, double num2);

	/**
	 * 根据结果和第一个操作数得到第二个操作数
	 * 
	 * @param result
	 *            结果
	 * @param num1
	 *            第一个操作数
	 * @return
	 */
	public abstract double calcNum2(double result, double num1);

	public static Operator valueOf(char op) {
		switch (op) {
		case '+':
			return ADD;
		case '-':
			return SUBTRACT;
		case '*':
			return MULTIPLY;
		case '/':
			return DIVIDE;

		default:
			throw new AssertionError("Unknown op: " + op);
		}
	}
}

 

import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.LinkedList;

/**
 * @Filename: NumberTest.java
 * @Description:
 * 
 * @author: luw.
 * @version: 1.0
 * @Create at: 2010-9-15 上午09:38:37
 */
public class NumberTest {

	/**
	 * @param args
	 * @throws IOException
	 */
	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		NumberCode[] nums = new NumberCode[3];
		Operator op = null;

		while (true) {
			System.out.println("Enter your value:");
			String str = br.readLine();
			str = str.replace(" ", "");

			long nanoTime = System.nanoTime();

			int j = 0;
			for (int i = 0; i < str.length(); i++) {
				char c = str.charAt(i);
				if (c == '=') {
					continue;
				}
				if (Character.isDigit(c)) {
					nums[j++] = NumberCode.valueOf(c);
				} else {
					op = Operator.valueOf(c);
				}
			}
			for (String s : moveMatch(op, nums[0], nums[1], nums[2])) {
				System.out.println(s);
			}
			System.out.println("用时 " + (System.nanoTime() - nanoTime) + "ns\n");
		}
	}

	public static Collection<String> moveMatch(Operator op, NumberCode num1, NumberCode num2, NumberCode result) {
		Collection<String> equations = new LinkedList<String>();
		int change = 1; // 可移动的火柴根数

		for (NumberCode n : num1.canConverts(change)) {
			if (op.calculate(n.intValue(), num2.intValue()) == result.intValue()) {
				equations.add(n.toString() + op + num2 + " = " + result);
			}
		}

		for (NumberCode n : num2.canConverts(change)) {
			if (op.calculate(num1.intValue(), n.intValue()) == result.intValue()) {
				equations.add(num1.toString() + op + n + " = " + result);
			}
		}

		for (NumberCode n : result.canConverts(change)) {
			if (op.calculate(num1.intValue(), num2.intValue()) == n.intValue()) {
				equations.add(num1.toString() + op + num2 + " = " + n);
			}
		}

		Collection<NumberCode> subNum1s = num1.subConverts(change);
		Collection<NumberCode> subNum2s = num2.subConverts(change);
		Collection<NumberCode> subResults = result.subConverts(change);
		for (NumberCode n1 : num1.addConverts(change)) {
			for (NumberCode n2 : subNum2s) {
				if (op.calculate(n1.intValue(), n2.intValue()) == result.intValue()) {
					equations.add(n1.toString() + op + n2 + " = " + result);
				}
			}

			for (NumberCode r : subResults) {
				if (op.calculate(n1.intValue(), num2.intValue()) == r.intValue()) {
					equations.add(n1.toString() + op + num2 + " = " + r);
				}
			}
		}

		for (NumberCode n2 : num2.addConverts(change)) {
			for (NumberCode n1 : subNum1s) {
				if (op.calculate(n1.intValue(), n2.intValue()) == result.intValue()) {
					equations.add(n1.toString() + op + n2 + " = " + result);
				}
			}

			for (NumberCode r : subResults) {
				if (op.calculate(num1.intValue(), n2.intValue()) == r.intValue()) {
					equations.add(num1.toString() + op + n2 + " = " + r);
				}
			}
		}

		for (NumberCode r : result.addConverts(change)) {
			for (NumberCode n1 : subNum1s) {
				if (op.calculate(n1.intValue(), num2.intValue()) == r.intValue()) {
					equations.add(n1.toString() + op + num2 + " = " + r);
				}
			}

			for (NumberCode n2 : subNum2s) {
				if (op.calculate(num1.intValue(), n2.intValue()) == r.intValue()) {
					equations.add(num1.toString() + op + n2 + " = " + r);
				}
			}
		}

		return equations;
	}
}

 

论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics