UVA 10023-square root (hand square root)

Source: Internet
Author: User

Title: UVA 10023-square root (hand squared root)

Topic links

To seek the square root of a given number.

Problem-solving ideas: With two points but this number is too big, it has timed out. After reading the problem, we find that we need to calculate the square root of a hand.

Algorithm:
First judge whether this number is an even digit, yes, the first time to take the previous two digits, not the first time only to take the previous one number as dividend. The next step is to calculate the two-bit two-bit for a section.
Using the previous calculation results multiply 20+ a single digit A and multiply this a, find the largest a so that the result of this formula is not greater than dividend.
Dividend subtract this result and then connect the tail with the next two digits of the large number as the new dividend.
Divisor is the tail of the original divisor connected to this a as a new divisor.
Repeat the above steps until the last two digits of this large number are calculated. The final divisor is the root number.

Code:

Importjava.util.*;Importjava.math.*;Importjava.io.*; Public  class Main {     Public StaticBigIntegersqrt(BigInteger ans)           {BigInteger num = Biginteger.zero;        BigInteger res = Biginteger.zero;        BigInteger Div; String str ="0"+ ans.tostring ();intLen = Str.length ();inti = len%2; for(; i < len; i + =2) {num = num.multiply (biginteger.valueof ( -). Add (NewBigInteger (str.substring (i, i +2))); div = res.multiply (biginteger.valueof ( -)); for(intj =0; J <Ten; J + +) {if(Div.add (biginteger.valueof (j +1). Multiply (biginteger.valueof (j +1). CompareTo (num) >0) {num = Num.subtract (Div.add (Biginteger.valueof (j)). Multiply (Biginteger.valueof (j))); res = res.multiply (biginteger.valueof (Ten). Add (Biginteger.valueof (j)); Break; }            }        }returnRes } Public Static void Main(String args[]) {Scanner cin =NewScanner (system.in);intT = Cin.nextint (); while(T >0) {BigInteger n = cin.nextbiginteger ();            System.out.println (sqrt (n)); t--;if(T >0) System.out.println (); }    }}

UVA 10023-square root (hand square root)

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.