九个数字

/**
 * 九个数字:
 * 功能:实现填充。一个四位数乘以一个一位数,得到一个四位数。这九个数分别用1~9这九个数填充。
 */
public class NineNumbers{

	public static void main(String[] args){
		
		int product = 0; 
		
		for(int i = 1234; i <= 9876; i++){	
			if(!theThouthandNumberWithoutSameNumberAndZero(i)){
				continue;
			}
			for(int j = 1; j <= 9; j++){
				if(theThouthandNumberWithTheOne(i, j)){
					continue;
				}
				product = i * j;
				if(!isThouthandNumber(product)){
					continue;
				}				
				if(!theThouthandNumberWithoutSameNumberAndZero(product)){
					continue;
				} 				
				if(theThouthandNumberWithTheOne(product, j)){
					continue;
				}				
				if(theTwoThouthandNumberWithoutSameNumber(i, product)){
					System.out.println("^_^ : I know " + i + " * " + j + " = " + product);
				}
			}
		}
	}
	
	/**
	 * 判断一个数是不是一个四位数
	 */
	public static boolean isThouthandNumber(int number){
		int thouthand = number/1000;
		if(thouthand > 9 || thouthand < 1){
			return false;
		}
		return true;
	}
	
	/**
	 * 判断四位数里面有没有跟一个一位数相同的数字
	 */
	public static boolean theThouthandNumberWithTheOne(int thoutandNumber, int one){
		int number_thousand = thoutandNumber/1000;
		int number_hundred = thoutandNumber%1000/100;
		int number_ten = thoutandNumber%100/10;
		int number_one = thoutandNumber%10;
		if(one == number_thousand || one == number_hundred || one == number_ten || one == number_one){
			return true;
		}
		return false;
	}
	
	/**
	 * 判断两个四位数不含有相同的数字
	 */
	public static boolean theTwoThouthandNumberWithoutSameNumber(int number1, int number2){
		int number1_thousand = number1/1000;
		int number1_hundred = number1%1000/100;
		int number1_ten = number1%100/10;
		int number1_one = number1%10;
		
		int number2_thousand = number2/1000;
		int number2_hundred = number2%1000/100;
		int number2_ten = number2%100/10;
		int number2_one = number2%10;
		
		boolean[] signs = new boolean[10];
		
		signs[number1_thousand] = true;
		signs[number1_hundred] = true;
		signs[number1_ten] = true;
		signs[number1_one] = true;	
		if(signs[number2_thousand]){
			return false;
		} 		
		if(signs[number2_hundred]){
			return false;
		} 		
		if(signs[number2_ten]){
			return false;
		}		
		if(signs[number2_one]){
			return false;
		} 
		
		return true;
	}
	
	/**
	 * 判断一个四位数没有相同数字且不含有零
	 */
	public static boolean theThouthandNumberWithoutSameNumberAndZero(int number){
	
		int number_thousand = number/1000;
		int number_hundred = number%1000/100;
		int number_ten = number%100/10;
		int number_one = number%10;
		boolean[] signs = new boolean[10];	
		
		signs[number_thousand] = true;		
		if(!signs[number_hundred]){
			signs[number_hundred] = true;
		} else {
			return false;
		}		
		if(!signs[number_ten]){
			signs[number_ten] = true;
		} else {
			return false;
		}		
		if(!signs[number_one]){
			signs[number_one] = true;
		} else {
			return false;
		}
		if(signs[0]){
			return false;
		}
		
		return true;
	}
}


你可能感兴趣的:(九个数字)