题解 | #求平方根#

求平方根

http://www.nowcoder.com/practice/09fbfb16140b40499951f55113f2166c

import java.util.*;

public class Solution { /** * * @param x int整型 * @return int整型 */ public int sqrt (int x) { // write code here int currentX = x; int iter = 0; while (iter <= 50 && currentX > 0){ // System.out.println((currentX + (x/currentX))/2); int temp = currentX; currentX = (int)Math.floor(((currentX + (x/currentX))/2)); if (currentX >= temp){ return temp; } iter += 1; } return currentX; } }

全部评论

相关推荐

头像
03-18 09:09
Java
点赞 评论 收藏
转发
头像
04-09 14:29
Java
点赞 评论 收藏
转发
点赞 收藏 评论
分享
牛客网
牛客企业服务