第一种解法:
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);
}
}
第二种解法:
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);
}
}