141 x的平方根
原题网址:http://www.lintcode.com/zh-cn/problem/sqrtx/
实现 int sqrt(int x) 函数,计算并返回 x 的平方根。
sqrt(3) = 1
sqrt(4) = 2
sqrt(5) = 2
sqrt(10) = 3
O(log(x))
#include <iostream>
#include <vector>
#include <math.h>
#include <string>
#include <algorithm>
using namespace std; int sqrt(int x)
{
if (x<)
{
return -;
} long long low=,high=x,mid=(low+high)/; //为何用int会出错?;
while (low<=high) //mid*mid有可能超出int范围被截断,小于x,若(mid+1)*(mid+1)被截断后仍小于x,返回错误结果;
{
if (mid*mid==x)
{
return mid;
}
else if (mid*mid>x)
{
high=mid-;
}
else
{
if ((mid+)*(mid+)>x)
{
return mid;
}
else
low=mid+;
}
mid=(low+high)/;
} }
参考:
1 https://blog.csdn.net/gao1440156051/article/details/49766733
2 http://www.cnblogs.com/AnnieKim/archive/2013/04/18/3028607.html
141 x的平方根的更多相关文章
- 九章lintcode作业题
1 - 从strStr谈面试技巧与代码风格 必做题: 13.字符串查找 要求:如题 思路:(自写AC)双重循环,内循环读完则成功 还可以用Rabin,KMP算法等 public int strStr( ...
- lintcode 刷题 by python 总结(1)
博主之前在学习 python 的数据结构与算法的基础知识,用的是<problem-solving-with-algorithms-and-data-structure-using-python& ...
- 141. Sqrt(x)【牛顿迭代法求平方根 by java】
Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...
- 牛顿法求平方根 scala
你任说1个整数x,我任猜它的平方根为y,如果不对或精度不够准确,那我令y = (y+x/y)/2.如此循环反复下去,y就会无限逼近x的平方根.scala代码牛顿智商太高了println( sqr(10 ...
- [LeetCode] Sqrt(x) 求平方根
Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...
- hdu 4027 2011上海赛区网络赛G 线段树 成段平方根 ***
不能直接使用成段增减的那种,因为一段和的平方根不等于平方根的和,直接记录是否为1,是1就不需要更新了 #include<cstdio> #include<iostream> # ...
- csuoj 1114: 平方根大搜索
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1114 1114: 平方根大搜索 Time Limit: 5 Sec Memory Limit: ...
- ocp 1Z0-051 141题---感觉有问题
141. View the Exhibit and examine the structure of CUSTOMERS and GRADES tables. You need to display ...
- leetcode 141
141. Linked List Cycle Given a linked list, determine if it has a cycle in it. Follow up:Can you sol ...
随机推荐
- linux redis安装及JAVA使用jedis
一.redis安装 1.安装redis 将redis安装包放到指定目录下.并用tar -zxvf redis.*****.tar.gz解压2.想把redis安装到哪里,就在哪里创建redis文件夹. ...
- vue 学习五 深入了解components(父子组件之间的传值)
上一章记录了 如何在父组件中向子组件传值,但在实际应用中,往往子组件也要向父组件中传递数据,那么此时我们应该怎么办呢 1.在父组件内使用v-on监听子组件事件,并在子组件中使用$emit传递数据 // ...
- Stack&Heap的理解
Heap(堆):在英文中有杂乱的堆意思,意译中文为堆:其特点为先进先出. 堆空间分配:一般由程序员分配释放, 若程序员不释放,程序结束时可能由OS回收,分配方式倒是类似于链表. Stack(栈):在英 ...
- 通过js渲染高层级DOM实现网页加水印
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Go 算术运算符
Go 算术运算符 package main import "fmt" func main() { var a int = 21 var b int = 10 var c int c ...
- Feign连接超时 Read timed out
在远程调用的过程中由于连接超时,导致无法成功请求数据,下面是报错 项目中用的是spring-cloud-starter-openfeign 2.2.0版本 找到对应的文档,开始查阅资料 文档首页就找到 ...
- NX二次开发-bat脚本文件切换NX的环境变量(NX路径和语言)
路径环境变量切换到NX9.bat @echo off setx /M UGII_BASE_DIR "D:\Program Files\Siemens\NX 9.0" ------- ...
- JsJquery小技巧
JS对URL编码 :encodeURI() .Net对URL解码:HttpUtility.UrlDecode() 格式化输出百分数 function formatePercent(data){ ...
- hdu6110
#include <cstdio> #include <iostream> #include <cmath> #include <cstring> #i ...
- DLL注入技术之劫持进程创建注入
劫持进程创建注入原理是利用Windows系统中CreateProcess()这个API创建一个进程,并将第6个参数设为CREATE_SUSPENDED,进而创建一个挂起状态的进程,利用这个进程状态进行 ...