This is the 11th day of my participation in the Gwen Challenge in November. Check out the details: The last Gwen Challenge in 2021

The 9th Blue Bridge Cup Java Province Competition Group B – Square count

Topic describes

As shown in the p1.png image, there are an infinite number of small 1×1 squares on the two-dimensional plane.

Let’s draw a circle with a radius of 1000 centered on one vertex of a small square.

Can you calculate how many complete squares there are in this circle?

Note: The submission is an integer, do not fill in any extra content.

Their thinking

I had the wrong idea… You take the area of the circle and you round it down.

In fact, this is not the number of complete squares, which is much less than the actual correct answer, because it would be that the two semicircles are one circle.

Later checked some other people’s analysis, this problem is still very easy……

The squares in the circle need to be complete, not the number of squares in the actual area. So once you understand that, it’s pretty easy to figure out how big this is.

We set up rectangular coordinates from the center of the circle.

You can determine if the square is complete in the circle by just checking whether the distance from the center is less than the radius of the circle.

The program code

package day_01;

public classBox counting{

	public static void main(String[] args) {
		/* We draw a circle with a radius of 1000 centered on one vertex of a small square. Can you calculate how many complete squares there are in this circle? * /

		int r=1000;
		int sum=0;
		
		for(int x=1; x<=r; x++) {for(int y=1; y<=r; y++) {if((x*x+y*y)<=r*r) {
					sum++;
				}
			}
		}
		
		System.out.print(sum*4); }}Copy the code

Code key Steps

Two for loops, respectively judge whether the relationship between x axis and y axis is consistent.

for(int x=1; x<=r; x++) {for(int y=1; y<=r; y++) {if((x*x+y*y)<=r*r) { sum++; }}}Copy the code

The results demonstrate

The result is: 3137548