求一个数的平方根(向下取整)

#include <stdio.h>

int test(int n)

{

if (n <= 1)

return n;

int begin = 1;

int end = n / 2;

int middle;

while (begin <= end)

{

middle = (begin + end) / 2;

if (middle == n / middle)

return middle;

else

{

if (middle < n / middle)

begin = middle + 1;

else

end = middle - 1;

}

}

return end;

}

int main()

{

int n = 0;

printf("请输入一个数\n");

scanf("%d", &n);

int res = test(n);

printf("%d\n",res);

return 0;

}

上一篇:「AGC 053」B - Taking the middle


下一篇:检测一个对象的所有属性是否都有值不为空