求平方根,正根.曾经都不会.昨天看数学,看到了,写了出来.自己又小优化了一下,非常不错.

// squareRoot.cpp -- 2011-08-29-01.04
#include "stdafx.h"
#include <iostream> double squareRoot (double radicand, double precision) ; int _tmain(int argc, _TCHAR* argv[])
{
std ::cout << squareRoot(9, 0.000001) << std ::endl ; return 0;
} double squareRoot (double radicand, double precision)
{
if (radicand > 0)
{
double squareRoot = radicand / 10 ;
while (squareRoot * squareRoot > radicand)
squareRoot /= 2 ;
double fakePrecision = 0.1;
while (1)
{
while ((squareRoot + fakePrecision) * (squareRoot + fakePrecision) <= radicand)
{
squareRoot += fakePrecision ;
}
if (fakePrecision > precision)
{
fakePrecision /= 10 ;
}
else
{
return squareRoot ;
}
}
}
else
{
std ::cerr << "Radicand must > 0" << std ::endl ;
return 0 ;
}
}

求平方根C++的更多相关文章

  1. [LeetCode] Sqrt(x) 求平方根

    Implement int sqrt(int x). Compute and return the square root of x. 这道题要求我们求平方根,我们能想到的方法就是算一个候选值的平方, ...

  2. ytu 1041: 迭代法求平方根(水题)

    1041: 迭代法求平方根 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 227  Solved: 146[Submit][Status][Web Bo ...

  3. 19. 求平方根序列前N项和

    求平方根序列前N项和 #include <stdio.h> #include <math.h> int main() { int i, n; double item, sum; ...

  4. [转载]求平方根sqrt()函数的底层算法效率问题

    我们平时经常会有一些数据运算的操作,需要调用sqrt,exp,abs等函数,那么时候你有没有想过:这个些函数系统是如何实现的?就拿最常用的sqrt函数来说吧,系统怎么来实现这个经常调用的函数呢? 虽然 ...

  5. note 5 二分法求平方根,素数,回文数

    +二分法求平方根 x = float(raw_input('Enter the number')) low = 0 high = x guess = (low + high ) / 2 if x &l ...

  6. C++版 - Leetcode 69. Sqrt(x) 解题报告【C库函数sqrt(x)模拟-求平方根】

    69. Sqrt(x) Total Accepted: 93296 Total Submissions: 368340 Difficulty: Medium 提交网址: https://leetcod ...

  7. JustOj 1036: 习题6.11 迭代法求平方根

    题目描述 用迭代法求 .求平方根的迭代公式为: X[n+1]=1/2(X[n]+a/X[n]) 要求前后两次求出的得差的绝对值少于0.00001. 输出保留3位小数 输入 X 输出 X的平方根 样例输 ...

  8. 二分法求平方根(Python实现)

    使用二分法(Bisection Method)求平方根. def sqrtBI(x, epsilon): assert x>0, 'X must be non-nagtive, not ' + ...

  9. 141. Sqrt(x)【牛顿迭代法求平方根 by java】

    Description Implement int sqrt(int x). Compute and return the square root of x. Example sqrt(3) = 1 ...

随机推荐

  1. 奇妙的CSS之伪类与伪元素

    我们都知道,在CSS中有很多选择器,例如id(#), class(.),属性[attr],这些虽然可以满足一些需要,但有时候还力有未逮.伪类和伪元素就提供了一个有益的补充,可以使我们更高效编码.伪类和 ...

  2. HDU 4632 Palindrome subsequence(区间DP求回文子序列数)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4632 题目大意:给你若干个字符串,回答每个字符串有多少个回文子序列(可以不连续的子串).解题思路: 设 ...

  3. LeetCode517. Super Washing Machines

    You have n super washing machines on a line. Initially, each washing machine has some dresses or is ...

  4. C# 图片和二进制之间的转换

    1> 图片转二进制  public byte[] GetPictureData(string imagepath){/**/////根据图片文件的路径使用文件流打开,并保存为byte[] Fil ...

  5. CRLF LF CR

    The Carriage Return (CR) character (0x0D, \r) moves the cursor to the beginning of the line without ...

  6. inotify 与 rsync文件同步实现与问题

    首先分别介绍inotify 与 rsync的使用,然后用两者实现实时文件同步,最后说一下这样的系统存在什么样的问题. 1. inotify 这个具体使用网上很多,参考 inotify-tools 命令 ...

  7. 【Java网络编程】基于 UDP 的聊天通信

    使用 udp 协议,写一个基于命令行的聊天软件:客户端跟服务端分别在命令行启动之后,客户端和服务器端可以互相发送数据. 代码实现如下: 一.创建线程 sendThread 和 receiveThrea ...

  8. 自定义Counter使用

    自定义计数器的使用(记录敏感单词) package counter; import java.net.URI; import org.apache.hadoop.conf.Configuration; ...

  9. matlab .fig转化成pdf 缺失

    1. 在matlab figure里面,Edit -> Copy Figure 2. 粘贴到ppt中即可

  10. 洛谷P2597 [ZJOI2012] 灾难 [拓扑排序,LCA]

    题目传送门 灾难 题目描述 阿米巴是小强的好朋友. 阿米巴和小强在草原上捉蚂蚱.小强突然想,如果蚂蚱被他们捉灭绝了,那么吃蚂蚱的小鸟就会饿死,而捕食小鸟的猛禽也会跟着灭绝,从而引发一系列的生态灾难. ...