zoukankan      html  css  js  c++  java
  • 大数开根号java模板

    利用逼近的思路直接二分开方找出值

     1 package lanqiao;
     2 
     3 import java.math.BigInteger;
     4 import java.util.Scanner;
     5 public class Main {
     6 
     7     static BigInteger cal(BigInteger x){
     8         BigInteger l = BigInteger.ONE ;
     9         BigInteger r = x ;
    10         BigInteger temp = BigInteger.ZERO ;
    11         while(!l.equals(r)){
    12             BigInteger mid = (l.add(r)).divide(BigInteger.valueOf(2)) ;
    13             if(temp.compareTo(BigInteger.ZERO)!=0&&temp.compareTo(mid)==0){
    14                 break ;
    15             }else{
    16                 temp = mid ;
    17             }
    18             if(temp.compareTo(BigInteger.ZERO)==0){
    19                 temp = mid ;
    20             }
    21             if(mid.multiply(mid).compareTo(x)==1){
    22                 r=mid ;
    23             }else{
    24                 l=mid ;
    25             }
    26         }
    27         if(l.multiply(l).compareTo(x)==1){
    28             l=l.subtract(BigInteger.ONE) ;
    29         }
    30         return l;
    31         
    32     }
    33     public static void main(String[] args) {
    34         // TODO Auto-generated method stub
    35         Scanner in = new Scanner(System.in) ;
    36         
    37         while(in.hasNextBigInteger()){
    38             BigInteger n = in.nextBigInteger() ;
    39             
    40             
    41             BigInteger x = cal(n) ;
    42             System.out.println(x);
    43             
    44         }
    45     }
    46 
    47 }
  • 相关阅读:
    简单说说数据库表设计的三种范式
    存储过程简单的动态订单号
    Asp.Net页面生命周期
    jq 小笔记,上传判断其格式
    吃一垫长一智
    离散事件模拟
    二叉树查找树
    冷暖自知
    基督徒的人生箴言
    迷宫寻路
  • 原文地址:https://www.cnblogs.com/Annetree/p/6664383.html
Copyright © 2011-2022 走看看