The Java "Tij" Java implementation finds all 4-digit vampire digits __java

Source: Internet
Author: User
What is the vampire number.


A vampire number is a number that has an even number of digits, and can be multiplied by a pair of numbers, and this pair of digits contains the half digits of the product,

The number that is selected from the initial number can be sorted arbitrarily. The number ending with two 0 is not allowed,

For example, the following numbers are "vampire" numbers:

1260 = 21 * 60

1827 = 21 * 87

2187 = 27 * 81


Programming Ideas


The first step , first, is to split the given four-digit number, to get each digit, into an array; for example, four digits "1234",

Divided into 1,2,3,4 four digits stored in the array;

The second step , the first step is divided into a total of 4 digits on each digit, according to its stored in the array of subscript (respectively, 0,1,2,3)

4 numbers are divided into two groups for permutation and grouping, which can be divided into: ((0,1), (2,3)), ((0,2), (1,3)), ((0,3), (1,2))

This will include a two-digit factor generating function combinedigit, which is based on the two digits provided and then gets the two

The number can be grouped into all two digits and stored in an array.

In the third step , each combination is judged to see if it can be composed of 2 two-digit factors A, B, which meet the "vampire number" requirement.

and A * B = original four-digit number


/** * Find 4 digits of all "vampire numbers" * Thinking in Java Test 4.10 * @author: Wolfofsiberian * @email: QuietWolf@yeah.net/public class				
	tij_test4_10{static final int numberdigit=4;

	static int splitarray[]=new Int[numberdigit]; /** * Extracts each digit of the four-digit number and saves it to the array * @param fourdigit a given four-digit number * @return void/public static void Splitnumber (int
		Fourdigit) {splitarray[0]=fourdigit%10;
		splitarray[1]= (FOURDIGIT/10)%10;
		splitarray[2]= (fourdigit/100)%10;
	splitarray[3]= (fourdigit/1000)%10;    /** * Combines the number of possible two digits into a given 2 digits, and the result of the combination is stored in array A * @param A1 split four digits 1 * @param A2 four digits in the number 2 * @param A has saved the combination of successful two digits * @return The value returned is the combination of the number of successful two digits stored in array A/private static int combinedigit (int a1,int a2,int a[]) {//if the number
		are 0, this is directly not eligible, return 0 if (a1 = = 0 && A2 = = 0) {returns 0;
			If one of the else{//2 digits is 0, consider that 0 cannot begin as a value. if (a1 = = 0 | | a2 = 0) {a[0] = (A1 = = 0)?

				(A2 *):(A1 * 10);	
			return 1; else{//If two numbers are 0, then there are two ways to combine them into a number a[0]= A1 * + A2;

				A[1] = A1 + A2 * 10;
			return 2; }}/** * To determine whether there is a "vampire number" requirement under the current combination * Build the idea: Fourdigit =?    A * B * @param fourdigit given a four-digit number, which is to be judged as "vampire number" * @param ai AI is the subscript index in array splitarray for building a factor * @param Aj AJ is the subscript index in array splitarray, which is used to construct a factor * @param bi bi is the subscript index in the array splitarray, which is used to construct the B factor * @param BJ BJ is the subscript index in the array splitarray.
	Used to construct the B-factor * @param rearr the array to save the A-factor and the B-factor value of the given four-digit "vampire number" * @return returns True, indicating that the given four digits under the current combination are "vampire numbers";
	* Returns false indicating that the current combination is not a "vampire number." * @since 1.0 */public static Boolean isxxgdigitcombine (int fourdigit,int ai,int aj,int bi,int bj,int rearr[]) {int
		A[]=new int[2];

		int b[]=new int[2];
		int Lena=combinedigit (SPLITARRAY[AI],SPLITARRAY[AJ],A);
		A factor combination failure if (0 = LenA) {return false;
		int Lenb=combinedigit (SPLITARRAY[BI],SPLITARRAY[BJ],B);
		b Factor combination Failure if (0 = LenB) {return false; for (int i=0;i<lena;i++) {for (int j=0;j<lenb;j++) {if (Fourdigit = = a[i] * B[j]) {//FindTo a combination that meets the requirements, saves to the array, and returns true rearr[0] = A[i];
					REARR[1] = B[j];
				return true; 
	}} return false; /** * Judge the current combination, whether there is a "vampire number" requirements * Build ideas: Fourdigit =? A * b * @param fourdigit a given four-digit * @param rearr the array to save the A-factor and the B-factor value of the given four-digit "vampire number" @return returns True, representing the given
	The four digits are "vampire numbers";
	* Returns FALSE, indicating that it is not a "vampire number".

		* @since 1.0 */public static Boolean isxxgdigit (int fourdigit,int rearr[]) {Boolean bresult=false;
		Step1 extracts a given four-digit number per digit to be saved in an array splitnumber (FOURDIGIT);
		
		Step2 2 2 digits do not consider the ordering of numbers in the two-digit factor, there are three combinations of these, as long as there is a combination of the conditions of the vampire numbers, all said that this four-digit number is consistent with the requirements. Return (Isxxgdigitcombine (fourdigit,0,1,2,3,rearr) | | | Isxxgdigitcombine ( Fourdigit,0,2,1,3,rearr) | |
	Isxxgdigitcombine (Fourdigit,0,3,1,2,rearr));
		public static void Main (String args[]) {int rearr[]=new int[2];

		int count=0; for (int i=1000;i<10000;i++) {if tij_test4_10.isxxgdigit (i,rEarr)) {count++;
			System.out.println (i+ "=" +rearr[0]+ "*" +rearr[1]); }
		}
	}
}

Run Result:

1260 = 60 * 21
1395 = 15 * 93
1435 = 35 * 41
1530 = 30 * 51
1827 = 87 * 21
2187 = 27 * 81
6880 = 80 * 86


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.