Java combat 15: square counting
Blue Bridge Cup java provincial group A 4.
Original title:
Title: Grid count
As shown in figure p1.png, there are countless 1x1 small squares on the two-dimensional plane.
We draw a circle with a radius of 50000 with a vertex of a small square as the center.
Can you calculate how many complete small squares are in this circle?
Note: What needs to be submitted is an integer, do not fill in any extra content.
Idea: Just look at how many complete squares there are in the first quadrant, and the distance from the top right corner of a square to the center of the circle is not less than r.
code show as below:
public class province_4 {
public static void main(String[] args) {
long l=50000;long t=0;
for(long i=1;i<l;i++) {
for(long j=1;j<l;j++) {
if(i*i+j*j<=l*l) {
t++;
}
}
}
System.out.println(t*4);
}
}
Operation result:
Note: Why use long for i can not use int: because i is in the range of int, but i*i is very large, not in the range of int, so the long type must be used. The same is true for j and r.
t itself is a very large number, so the long type is used.
For two-number operations, the data type of the result of the operation is determined by the higher priority of the two-number priority.