ACM 356 Square Pegs And Round Holes
作法:目前有沒好想法 (由於N很小 所以決定暴力)
舉出1/4圓的所有可能,速度很慢(待改良)
/****************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
main()
{
int n;
while(scanf("%d",&n)==1)
{
double r=(2*n-1)/2.0;
int a,b,all=0,half=0;
for(a=0;a<=(int)r+1;a++)
for(b=0;b<=(int)r+1;b++)
{
double a1,a2,a3,a4;
a1=sqrt(a*a+b*b);
a2=sqrt((a+1)*(a+1)+b*b);
a3=sqrt(a*a+(b+1)*(b+1));
a4=sqrt((a+1)*(a+1)+(b+1)*(b+1));
if(a1<=r&&a2<=r&&a3<=r&&a4<=r)
all++;
else if(a1>r&&a2>r&&a3>r&&a4>r)
break;
else half++;
}
printf("In the case n = %d, %d cells contain segments of the circle.\n",n,half*4);
printf("There are %d cells completely contained in the circle.\n",all*4);
}
return 0;
}