UVa10023手动开大数平方算法

题目链接:UVa 10023

 1 import java.math.BigInteger;
 2 import java.util.Scanner;
 3 public class Main {
 4     public static void sqrt(BigInteger bi){
 5         String str;
 6         str=bi.toString();
 7         int m=str.length();
 8         if(m%2!=0)
 9         str="0"+str;
10         BigInteger a,b,c,d,ans;
11         b=BigInteger.valueOf(0);
12         c=BigInteger.valueOf(0);
13         ans=BigInteger.valueOf(0);
14         try{
15             for(int i=0;i<m;i+=2){
16                 a=b.multiply(new BigInteger("100")).add(new BigInteger(str.substring(i,i+2)));
17                 for(int j=0;j<10;j++){
18                     d=c.multiply(new BigInteger("20")).add(BigInteger.valueOf(j+1)).multiply(BigInteger.valueOf(j+1));
19                     if(d.compareTo(a)>0){
20                         c=c.multiply(new BigInteger("20")).add(BigInteger.valueOf(j)).multiply(BigInteger.valueOf(j));
21                         b=a.subtract(c);
22                         ans=ans.multiply(new BigInteger("10")).add(BigInteger.valueOf(j));
23                         c=ans;
24                         
25                         break;
26                     }
27                 }
28             }
29         }catch(Exception e){
30             e.getStackTrace();
31         }
32         System.out.println(ans);
33     }
34     public static void main(String args[]){
35         Scanner cin=new Scanner(System.in);
36         int n;
37         n=cin.nextInt();
38         for(int k=0;k<n;k++){
39             if(k!=0)
40                 System.out.println();
41             sqrt(cin.nextBigInteger());
42         }
43     }
44 }

 

 

 

 

 

 

 

 

 

 

原文地址:https://www.cnblogs.com/mypsq/p/4723855.html