The following program is my ACcepted code for UVA-477.
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UAV 477的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
//This program is for UVA 477 Points in Figures Rectangles and Circles
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
int numF = 0;// for number of Figure
int numP = 0;// for number of points
char figType[11];
double rec[11][2][2];//[0][0] and [0][1] is the upper left , [1][0] and [1][1] is the lower right
double circle[1][3];//[0] is x, [1] is y, [2] is radius
int main()
{
numF = 0;
numP = 0;
char tmpChar;
double x,y, tmpX, tmpY;
while(scanf("%c", &tmpChar)!=EOF)
{
if(tmpChar == '*')
break;
numF ++;// from 1 to the number of rectangle
figType[numF] = tmpChar;
if(tmpChar == 'r')
scanf("%lf %lf %lf %lf\n", &rec[numF][0][0], &rec[numF][0][1], &rec[numF][1][0], &rec[numF][1][1]);
//[0][0] and [0][1] is the upper left , [1][0] and [1][1] is the lower right
else if(tmpChar == 'c')
scanf("%lf %lf %lf\n", &circle[numF][0], &circle[numF][1], &circle[numF][2]);
//[0] is x, [1] is y, [2] is radius
}
while(scanf("%lf %lf", &x, &y)!=EOF)
{
numP++;// from 1 to the number of points
//To compare the double, we use the absolute value, use float would fail
if( fabs(x-9999.9)<=0.0000001 && fabs(y-9999.9)<=0.0000001 )
break;
bool findAns = false;
for(int i=1; i<= numF; i++)
{
if(figType[i] == 'r')
{
if(x > rec[i][0][0] && x < rec[i][1][0] && y > rec[i][1][1] && y < rec[i][0][1])
{
findAns = true;
printf("Point %d is contained in figure %d\n", numP, i);
}
}
if(figType[i] == 'c')
{
tmpX = fabs(x - circle[i][0]);// distance of x and center's x of circle
tmpY = fabs(y - circle[i][1]);
tmpX *= tmpX;
tmpY *= tmpY;
if( (tmpX + tmpY) < (circle[i][2]*circle[i][2]) )
{
findAns = true;
printf("Point %d is contained in figure %d\n", numP, i);
}
}
}
if(findAns == false)
printf("Point %d is not contained in any figure\n", numP);
}
return 0;
}
It's a for everybody to learn and discuss.
If there is any mistake or comment, please let me know. :D
此乃UAV 477的AC code!
歡迎一同討論學習,如有錯誤與任何建議請留言 : )
//This program is for UVA 477 Points in Figures Rectangles and Circles
#include<stdio.h>
#include<iostream>
#include<stdlib.h>
#include<math.h>
using namespace std;
int numF = 0;// for number of Figure
int numP = 0;// for number of points
char figType[11];
double rec[11][2][2];//[0][0] and [0][1] is the upper left , [1][0] and [1][1] is the lower right
double circle[1][3];//[0] is x, [1] is y, [2] is radius
int main()
{
numF = 0;
numP = 0;
char tmpChar;
double x,y, tmpX, tmpY;
while(scanf("%c", &tmpChar)!=EOF)
{
if(tmpChar == '*')
break;
numF ++;// from 1 to the number of rectangle
figType[numF] = tmpChar;
if(tmpChar == 'r')
scanf("%lf %lf %lf %lf\n", &rec[numF][0][0], &rec[numF][0][1], &rec[numF][1][0], &rec[numF][1][1]);
//[0][0] and [0][1] is the upper left , [1][0] and [1][1] is the lower right
else if(tmpChar == 'c')
scanf("%lf %lf %lf\n", &circle[numF][0], &circle[numF][1], &circle[numF][2]);
//[0] is x, [1] is y, [2] is radius
}
while(scanf("%lf %lf", &x, &y)!=EOF)
{
numP++;// from 1 to the number of points
//To compare the double, we use the absolute value, use float would fail
if( fabs(x-9999.9)<=0.0000001 && fabs(y-9999.9)<=0.0000001 )
break;
bool findAns = false;
for(int i=1; i<= numF; i++)
{
if(figType[i] == 'r')
{
if(x > rec[i][0][0] && x < rec[i][1][0] && y > rec[i][1][1] && y < rec[i][0][1])
{
findAns = true;
printf("Point %d is contained in figure %d\n", numP, i);
}
}
if(figType[i] == 'c')
{
tmpX = fabs(x - circle[i][0]);// distance of x and center's x of circle
tmpY = fabs(y - circle[i][1]);
tmpX *= tmpX;
tmpY *= tmpY;
if( (tmpX + tmpY) < (circle[i][2]*circle[i][2]) )
{
findAns = true;
printf("Point %d is contained in figure %d\n", numP, i);
}
}
}
if(findAns == false)
printf("Point %d is not contained in any figure\n", numP);
}
return 0;
}
沒有留言:
張貼留言
請留下您的任何想法或建議!
Please leave any thought or comment!