Blue Bridge Cup Provincial Grid Counting

Insert picture description here
The first solution:

public class 方格计数 {
    
    

	public static void main(String[] args) {
    
    
		// TODO Auto-generated method stub
		long count=0;
		for(long i=-1000;i<=1000;i++) {
    
    
			for(long j=-1000;j<=1000;j++) {
    
    
				if(i!=0 && j!=0 && i*i+j*j<=1000*1000) {
    
    
					count++;
				}
			}
		}
		System.out.println(count);
	}

}

The second solution:

public class 方格计数 {
    
    
	public static void main(String[] args) {
    
    
		long max=1000;
		long count=0;
		for(int i=1;i<max;i++) {
    
    
			while(max*max+i*i>1000*1000) {
    
    max--;}
			count+=max;
			max=1000;
		}
		System.out.println(count*4);
	}
}

Guess you like

Origin blog.csdn.net/a12355556/article/details/115038894