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 ...
随机推荐
- Go学习笔记:初识Go语言
Go语言简介 Go语言是Google(谷歌)公司开发的一款静态型.编译型并自带垃圾回收机制和并发的编程语言. Go语言的风格类似于C语言.其语法在C语言的基础上进行了大幅的简化,去掉了不需要的表达式括 ...
- Java内存管理简述
转自:http://www.codeceo.com/article/java-memory-area.html 一.概述 Java虚拟机在执行Java程序的过程中会把它所管理的内存划分为若干不同的数据 ...
- VMWare 禁用虚拟内存文件(*.vmem)
1.使用 VMWare 虚拟机,虚拟机启动后,会在虚拟机目录下建立一个与虚拟内存大小相同的 .vmem文件,例如:564db13c-c92d-3d3a-41a0-f62af7536fda.vmem. ...
- c++内存相关函数
memset void *memset(void *s, int ch, size_t n); 函数解释:将s中当前位置后面的n个字节 (typedef unsigned int size_t )用 ...
- 去sqlserver日志
USE [master] GO ALTER DATABASE DNName SET RECOVERY SIMPLE WITH NO_WAIT GO ALTER DATABASE DNName SET ...
- 获取Delphi焦点所在的控件及通过控件名称访问控件
方法一: Var I: Integer; Begin For I := To ComponentCount - Do //获取组件数量 Begin If Components[I] Is TWinCo ...
- PDO基础
//PDO:数据访问抽象层 $dsn = "mysql:dbname=mydb;host=localhost";//造PDO对象 $pdo = new PDO($dsn," ...
- NX二次开发-UF_OBJ_cycle_by_name遍历名字
使用前自己要看好名字是加在body,还是face,还是curve,或者其他,别加错了. NX9+VS2012 #include <uf.h> #include <uf_obj.h&g ...
- NX二次开发-UFUN工程图表格注释检索默认单元格首选项UF_TABNOT_ask_default_cell_prefs
NX9+VS2012 #include <uf.h> #include <uf_tabnot.h> #include <NXOpen/Part.hxx> #incl ...
- 20140415 HOG 不同继承方式的访问特性 虚函数工作原理
1.HOG block重叠的好处 由于行人通常其形状可以视为柔体,人 的边缘位置不固定,而有一些移动,block 重叠后,一个边缘的梯度信息在两个相邻重叠的 block 中都能有所表达,这样即使边缘的 ...