赞
踩
- package datasSouce;
- //返回平方跟的整数(不能用sqrt)
- public class SqrtX {
- public static void main(String[] args) {
- System.out.println(binSearch(15));
- }
-
- private static int binSearch(int x) {
- //返回值
- int index = -1;
- //定义高低两个指针
- int low = 0, high = x;
- //当小的大于大的的时候退出循环
- while (low <= high){
- //定义一个中间值
- int mid = (low + high)/2;
- //mid的平方和x比较
- if (mid * mid <= x){
- index = mid;
- //小于x,mid+1
- low = mid + 1;
- }else {
- //大于x,mid-1
- high = mid - 1;
- }
- }
- return index;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。