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)