2011年12月26日 星期一

C ACM477 Points in Figures: Rectangles and Circles

[心得] : // runtime 0.012
判斷是否在圓內,利用距離公式,求出與圓心距離,是否小於半徑,是 則該點落於圓內!
距離公式: sqrt((pow(x2-x1),2)+pow((y2-y1),2)))
記得要 #include <math.h>

 #include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXSIZE  10
int main()
{
  double array[MAXSIZE ][MAXSIZE ];
  double a, b;
  char s[MAXSIZE];
  int m=0;
  int i,j;
  int counter =1;
  int flag=0;
  while(scanf("%s",&s[m])==1){
    if(s[m]=='*')
       break;
    if(s[m] == 'r')                    
         scanf("%lf %lf %lf %lf",&array[m][0],&array[m][1],&array[m][2],&array[m][3]);
    else if(s[m] == 'c')                    
         scanf("%lf %lf %lf",&array[m][0],&array[m][1],&array[m][2]);
    m++;
                      
  } 
  while(scanf("%lf %lf",&a,&b)==2){
   if(a==9999.9 ||b==9999.9)
    break;
 
   for(i=0;i<=m;i++){
     if(s[i] == 'r'){
        if(a >array[i][0]&&a<array[i][2] && b>array[i][3]&&b <array[i][1]){
        flag =1;
        printf("Point %d is contained in figure %d\n",counter,(i+1));
       }
      }
     if(s[i] == 'c'){
      if(array[i][2]>sqrt(pow((a-array[i][0]),2)+pow((b-array[i][1]),2))){
        flag =1;
        printf("Point %d is contained in figure %d\n",counter,(i+1));
       }
     }
  }
    if(flag == 0)
     printf("Point %d is not contained in any figure\n",counter);
    counter++;
    flag = 0;
  }
   
system("pause"); 
//return 0;
}

沒有留言:

張貼留言