[编程题] 优雅的点
时间限制:1秒
空间限制:32768K
小易有一个圆心在坐标原点的圆,小易知道圆的半径的平方。小易认为在圆上的点而且横纵坐标都是整数的点是优雅的,小易现在想寻找一个算法计算出优雅的点的个数,请你来帮帮他。
例如:半径的平方如果为25
优雅的点就有:(+/-3, +/-4), (+/-4, +/-3), (0, +/-5) (+/-5, 0),一共12个点。
输入描述:
输入为一个整数,即为圆半径的平方,范围在32位int范围内。
输出描述:
输出为一个整数,即为优雅的点的个数
输入例子1:
25
输出例子1:
12
如果半径为整数的话,肯定上下左右有4个点。然后继续判断其他点,在0到半径的路径上,利用勾股定理判断是否有整数点存在,存在的话加4
import java.util.Arrays;
import java.util.*;
public class Main {
public static void main(String[] args)
{
Main main = new Main();
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
System.out.println(main.circle(n));
}
public int circle(int n)
{
int num;
boolean flag = (int) Math.sqrt(n)-Math.sqrt(n)==0;
if(flag)
num = 4;
else
num = 0;
for(int i=1;i<Math.sqrt(n);i++)
{
flag = (int) Math.sqrt(n-i*i)-Math.sqrt(n-i*i)==0;
if(flag)
{
num+=4;
}
}
return num;
}
}