jquery+CSS3 简易无限级纵向(上下)菜单插件

Sqrt(x)

 Total Accepted: 8010 Total Submissions: 37050My Submissions

Implement int sqrt(int x).

Compute and return the square root of x.


分二分法和牛顿迭代法两种解法,详细请看:

http://blog.csdn.net/kenden23/article/details/14543521


二分法:

//2014-2-20 update		
	int sqrt(int x) 
	{
		double e = 0.0000001;
		double a = 0, b = x;
		double m = a + ((b-a)/2);
		double res = m*m - double(x);
		while (abs(res) > e)
		{
			if (res < 0) a = m+1;
			else b = m-1;
			m = a + ((b-a)/2);
			res = m*m - double(x);
		}
		return m;
	}

牛顿迭代法:

//2014-2-20 update		
	int sqrt(int x) 
	{
		if (x == 0) return 0;
		double xi1 = x, xi = 1;
		while (int(xi1) != int(xi))
		{
			xi = xi1;
			xi1 = (double(x)/xi + xi)/2;
		}
		return xi;
	}




郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。