zoukankan      html  css  js  c++  java
  • 69. x 的平方根

    69. x 的平方根

    https://leetcode-cn.com/problems/sqrtx/description/

    package com.test;
    
    /**
     * @Author stono
     * @Date 2018/8/24 下午5:48
     */
    public class Lesson069 {
        public static void main(String[] args) {
            int x = 2147395600;
    //        System.out.println(Math.sqrt(Integer.MAX_VALUE));
    //        x = 81;
            int i = mySqrt(x);
    //        x = 4;
    //        while (x * x > 0) {
    //            System.out.println(x * x);
    //            x = x*4;
    //        }
    
            System.out.println(i);
    //        System.out.println(i*i);
    //        System.out.println(46341*46341);
        }
    
        public static int mySqrt(int x) {
            if (x == 0) {
                return 0;
            }
            int i = 1;
            int k = 4;
            while (k > 0) {
                // i的平方小于x,并且i小于最大整数的平方根
                while (i * i <= x && i<=46340) {
                    // k>1的时候,就进行相乘,加快逼近的步伐,乘以4已经很快逼近了;
                    if (k > 1) {
                        i = i * k;
                    }else{
                        // k==1的时候,就累加i进行逼近
                        i = i+1;
                    }
                }
                i = i / k;
                // 4倍超过了,改3倍,改2倍,最后变成1;
                k--;
            }
            return i - 1;
        }
    }
  • 相关阅读:
    Python 入门 之 print带颜色输出
    memcache缓存
    PDO
    面向对象(二)
    面向对象(一)
    文件上传
    简单的权限管理
    当前时间与时期联动
    淡入淡出、滑动、及遍历
    留言板相关功能
  • 原文地址:https://www.cnblogs.com/stono/p/9531569.html
Copyright © 2011-2022 走看看