Topic description
Xiaoyi has a circle whose center is at the origin of coordinates, and Xiaoyi knows the square of the radius of the circle. Xiaoyi thinks that the points on the circle and the points whose horizontal and vertical coordinates are integers are elegant. Xiaoyi now wants to find an algorithm to calculate the number of elegant points. Please help him.
For example: if the square of the radius is 25
elegant points have: (+/-3, +/-4), (+/-4, +/-3), (0, +/-5) (+/- 5, 0), a total of 12 points.
For example: if the square of the radius is 25
elegant points have: (+/-3, +/-4), (+/-4, +/-3), (0, +/-5) (+/- 5, 0), a total of 12 points.
Enter description:
The input is an integer, which is the square of the radius of the circle, and the range is in the range of 32-bit int.
Output description:
The output is an integer, which is the number of elegant points
Example 1
enter
25
output
12
1 import java.util.Scanner; 2 3 /** 4 * 5 * The elegant point on the circle and the coordinates are integers only need to calculate the first quadrant due to symmetry 6 * @author Dell 7 * 8 */ 9 public class Main { 10 public static void main(String[] args) { 11 Scanner sc = new Scanner(System.in); 12 // 2 squared 13 int r_2 = sc.nextInt(); 14 int n = 0 ; 15 // On the coordinate axis, either x=0 or y=0 and as long as one of the four numbers matches four, they all meet 16 // Just need to judge one and finally follow the four quadrants multiplied by 4 17 // The judgment range is 0<= x^ 2 < r^2 equal sign takes only one 18 int x = 0 ; 19 int y0 = 1 ; 20 while (x*x< r_2) { 21 double y_2 = r_2-x* x; 22 // y1 is of type double y^2 square root 23 double y1 = Math.sqrt(y_2); 24 // y0 is y1 forced to get 25 y0 = ( int )y1; 26 // By judging whether y0 y1 is equal to determine whether the y1 obtained from the square root is not for integer 27 if(y0== y1) { 28 n++ ; 29 } 30 x++ ; 31 } 32 // Four quadrants multiplied by four after the loop 33 n*=4 ; 34 System.out.println(n); 35 } 36 }