吸血鬼数字(无聊看JAVA的一个例子 好玩而已)

//
//	Think in Java
//	FindData.java
//	2012-1-31 11:56

package com.yangjie.finddata;

public class FindData {
	
	boolean AnalyzeData(int data) {
		if (data < 1000 || data > 9999)
			return false;
		
		int v0, v1, v2, v3;
		
		v0 = data % 10;
		v1 = (data / 10) % 10;
		v2 = (data / 100) % 10;
		v3 = (data / 1000) % 10;
	
		int [] arr = new int [4];
		arr[0] = v0;
		arr[1] = v1;
		arr[2] = v2;
		arr[3] = v3;
		
		for (int i = 0; i < 4; i ++) {
			
			int temp1 = arr[i];
			for (int j = 0; j < 4; j ++) {
				
				if (i != j) {
					
					int temp2 = arr[j];
					int [] temp = new int [2];
					int n = 0;
					for (int k = 0; k < 4; k ++) {
						
						if (k != i && k != j) {
							
							temp[n] = arr[k];
							n ++;
						}
					}
					
					int sum = (temp1 * 10 + temp2) * (temp[0] * 10 + temp[1]);
					if (sum == data) {
						
						System.out.println(data + "=" + (temp1 * 10 + temp2) + " * " + (temp[0] * 10 + temp[1]));
						return true;
					}
					
					sum = (temp1 * 10 + temp2) * (temp[0] + temp[1] * 10);
					if (sum == data) {
						
						System.out.println(data + "=" + (temp1 * 10 + temp2) + " * " + (temp[0] + temp[1] * 10));
						return true;
					}
				}
			}
		}
		
		return false;
	}
	
	public static void main(String[] args) {
		
		FindData fd = new FindData();
		for (int i = 1000; i < 9999; i ++) {
			
			fd.AnalyzeData(i);
		}
	}
}

你可能感兴趣的:(吸血鬼数字(无聊看JAVA的一个例子 好玩而已))