zoukankan      html  css  js  c++  java
  • 平方开根

    long long 范围内的开根

    int Sqrt(int x) {
        if (x == 0) return 0;
        double last = 0;
        double res = 1;
        while (res != last)
        {
            last = res;
            res = (res + x / res) / 2;
        }
        return int(res);
    }
    

     浮点数的开根

    double fun(double x) {
        if (x == 0) return 0;
        double last = 0.0;
        double res = 1.0;
        while (res != last)
        {
            last = res;
            res = (res + x / res) / 2;
        }
        return res;
    }
    

    java 大数套牛顿迭代

    import java.math.BigInteger;
    import java.math.*;
    import java.math.BigInteger;
    import java.util.Scanner;
    import java.util.*; 
    public class Main
    {
        public static void bigSqrt(){
             Scanner cin=new Scanner(System.in);
              String s=cin.next();
              BigInteger remain=BigInteger.ZERO;
              BigInteger odd=BigInteger.ZERO;
              BigInteger ans=BigInteger.ZERO;
    //          remain=BigInteger.ZERO;
    //          odd=BigInteger.ZERO;
    //          ans=BigInteger.ZERO;
              int group=0,k=0;
              if(s.length()%2==1)
              {
                      group=s.charAt(0)-'0';
                      k=-1;
              }
              else
              {
                      group=(s.charAt(0)-'0')*10+s.charAt(1)-'0';
                      k=0;
              }
              for(int j=0;j<(s.length()+1)/2;j++)
              {
                      if(j!=0)
                      group=(s.charAt(j*2+k)-'0')*10+s.charAt(j*2+k+1)-'0';
                      odd=BigInteger.valueOf(20).multiply(ans).add(BigInteger.ONE);
                      remain=BigInteger.valueOf(100).multiply(remain).add(BigInteger.valueOf(group));
                      int count=0;
                      while(remain.compareTo(odd)>=0)
                      {
                             count++;
                             remain=remain.subtract(odd);
                             odd=odd.add(BigInteger.valueOf(2));
                      }
                      ans=ans.multiply(BigInteger.TEN).add(BigInteger.valueOf(count));
              }
              System.out.println(ans);
            cin.close();
            return;
        }
           public static void main(String[] args) 
           {
                   Scanner cin=new Scanner(System.in);
                   //int t=cin.nextInt();
    
                       bigSqrt();
    
                  cin.close();
          }
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    linux 第五天
    linux 第四天
    二进制 位运算
    二进制
    java 方法的调用过程
    Linux 第三天
    Linux 第二天
    Linux
    学习了半个多月的TankGame
    学习第一天(spring)
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9651982.html
Copyright © 2011-2022 走看看