Evaluate vampire numbers using Java

Source: Internet
Author: User

A vampire number refers to a number with an even number. It can be obtained by multiplying a number, and each of the numbers contains one of the product.

A half-digit number. The number selected from the initial number can be sorted in any order. Digits ending with two zeros are not allowed.

For example, the following figures are all vampire numbers:

1260 = 21*60

1827 = 21*87

2187 = 27*81

The idea of writing this program is:

1. First, find the three numbers that satisfy this equation, and then select the number that is the vampire number. The following vampire

The function is to first find the equation and then call the comparestring () function to determine whether it is a vampire number.

public static void Vampire(){for(int i =999;i<9999;i++){if((i%100) == 0) continue;for(int j=11;j<100;j++)for(int k =11;k<j;k++){if(j*k == i){String si ="" +i;String sj ="" +j;String sk ="" +k;if(CompareString(si,sj,sk)){System.out.println(si +" =" +sj +"*"+sk);}}}}}

2. Calling the comparestring () function has three string parameters, indicating the product, multiplier, and multiplier respectively. During the judgment, the two multiplier values are converted into a string and then compared with the product. An sort () sorting function is used during the comparison.

Public static Boolean comparestring (string target, string str1, string str2) {int COUNT = 0; string strtemp = str1 + str2; char S1 [] = target. tochararray (); char S2 [] = strtemp. tochararray (); sort (S1); sort (S2); For (INT I = 0; I <s1.length; I ++) {If (S1 [I] = S2 [I]) {count ++ ;}} if (COUNT = s1.length) return true; // true is returned if each bit is the same; otherwise, falseelse return false ;}

3. The functions of the sort () function sort the numbers in the char array in ascending order. For example, if a char array is {'1', '2', '6', and '0'}, it is changed to {'0', '1' after the sort () function ', '2', '6 '}. so whether the last comparison is a vampire number is to compare whether the two strings are the same after being sorted. In fact, the strings of the two multiplier combinations and the product strings are only in different alphabetic order. After sorting, we can make a good comparison.

public static void Sort(char[] numArray){int length = 0;length = numArray.length;for(int i =1; i<length;i++){int j = i -1;char Temp  = numArray[i];while(j>=0&&Temp<numArray[j]){numArray[j+1] = numArray[j];j--;}numArray[j+1] = Temp;}}

Click to download the program source code

Another net user JAVA Implementation of efficient vampire digital algorithm: http://blog.csdn.net/java2000_net/article/details/3851203

At this point, you can determine whether it is a vampire number.

There is a very important idea for such problems by asking for the vampire numbers. For example:

String S1 = "abcdsddgff ";

String S2 = "ddfgascdb ";

This idea can be applied to determine whether the two strings contain the same letters and whether the numbers of the same letters are the same. You can sort the strings and then compare them. After sorting, S1 becomes abcdddffgs and S2 becomes abcdddffgs, which can be easily compared. There are many options for sorting algorithms.

 

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.