Judging self-guarding number

Source: Internet
Author: User

Title Description:
If the number of squares at the end of a number equals this number, then the number is called the self-hold number.
Obviously, 5 and 6 are a self-observing number (5x5=25 6x6=36), 25x25=625 76x76=5776, so 25 and 76 are two-bit self-guarding.
Enter a number to determine whether the number is a self-hold number.

The idea of solving problems: it is obviously undesirable to use the method of "finding the square of a number and then intercepting the last corresponding digit", because the computer cannot represent an oversized integer. So we take the idea of multiplying by large numbers to store the square of this number. Then we can judge whether the latter of the square term is equal to the original number.

Import Java.util.Scanner; PublicFinalclassDemo { Public Static void Main(string[] args) {//TODO auto-generated method stubScanner Scanner =NewScanner (System.inch); System. out. println ("Please num:");intn = scanner.nextint (); System. out. println (Isautomorphicnum (n)); } Public StaticBooleanIsautomorphicnum(intNUM) {if(Num <0){return false; }if(num = =0){return true; } String s = string.valueof (num);int[] Number =New int[S.length ()]; for(intI=0; I<s.length (); i++) {number[s.length ()-i-1] = S.charat (i)-' 0 ';//"12345", 5,4,3,2,1}int[] result = multi (number, number); String str =""; for(inti=result.length-1; i>=0; i--) {str + = Result[i]; } String str1 = num +"";if(Str1.equals (Str.substring (Str.length ()-str1.length ()))) {//Determine if the number is self-guarding            return true; }return false; }///The following method implements the multiplication of large numbers, saving each bit of the results in the result array     Public Static int[]Multi(intNum1[],intNum2[]) {intLen1 = Num1.length;intLen2 = Num2.length;int[] result =New int[Len1 + len2]; for(intI=0; i<len1;i++) { for(intj=0; j<len2;j++) {Result[i+j] + = num1[i]*num2[j]; }        } for(intI=0; i<result.length-1; i++) {if(Result[i] >Ten) {result[i+1] + = result[i]/Ten; Result[i]%=Ten; }        }returnResult }}

Judging self-guarding number

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.