点在哪问题
时间限制(普通/Java):5000MS/10000MS 运行内存限制:65536KByte
总提交:87 测试通过:40
总提交:87 测试通过:40
描述
给出一组图形(矩形或圆),和一组点的数据,判断点的位置。
输入
输入数据的第一行为测试用例个数,对每一个测试用例,每行以“c”开头表示圆,以“r”开头表示矩形,其中矩形是依次给出左下角和右上角的坐标,圆是给出圆心坐标及半径,图形数据以输入另起一行的“*”结束,接下来是给出点的坐标(x,y),每个测试用例以9999.9 9999.9结束。
输出
每个测试用例输出点的位置信息:如某点在图形上,则输出所有图形上的信息:“Point i is contained in figure j”,如某点不在任一图形内,则输出:“Point i is not contained in any figure”,每个用例以一空行隔开。
样例输入
2
r 0.0 0.0 5.5 10.3
c -5.0 -5.0 3.7
r 2.5 2.5 12.5 12.5
*
2.0 2.0
4.7 5.3
9999.9 9999.9
c 0.0 0.0 1.0
*
0.0 0.5
3.0 3.0
9999.9 9999.9
r 0.0 0.0 5.5 10.3
c -5.0 -5.0 3.7
r 2.5 2.5 12.5 12.5
*
2.0 2.0
4.7 5.3
9999.9 9999.9
c 0.0 0.0 1.0
*
0.0 0.5
3.0 3.0
9999.9 9999.9
样例输出
Point 1 is contained in figure 1
Point 2 is contained in figure 1
Point 2 is contained in figure 3
Point 1 is contained in figure 1
Point 2 is not contained in any figure
Point 2 is contained in figure 1
Point 2 is contained in figure 3
Point 1 is contained in figure 1
Point 2 is not contained in any figure
#include<iostream>
using std::cin;
using std::cout;
using std::endl;
#include<cmath>
const size_t MAX_SIZE = 1000;
const double END_NUMBER = 9999.9;
class Shape{
private:
char type;
double point_x, point_y, radius;
double point1_x, point1_y, point2_x, point2_y;
public:
void set(char t_type);
bool is_inside(double& t_point_x, double& t_point_y);
};
void Shape::set(char t_type){
type = t_type;
if (t_type == 'r'){
cin >> point1_x >> point1_y >> point2_x >> point2_y;
}
else if (t_type == 'c'){
cin >> point_x >> point_y >> radius;
}
}
bool Shape::is_inside(double& t_point_x, double& t_point_y){
if (type == 'c'){
double distence = sqrt(pow(t_point_x - point_x, 2.0) + pow(t_point_y - point_y, 2.0));
return distence <= radius;
}
else{
return (t_point_x >= point1_x && t_point_x <= point2_x)
&& (t_point_y >= point1_y && t_point_y <= point2_y);
}
}
int main(void){
Shape shape_lib[MAX_SIZE]; //图形数组
size_t i, size;
short int times;
char type;
double point_x, point_y;
bool has_inside;
short int point_counter;
bool is_first = true;
scanf("%hd", ×);
while (times--){
i = 0;
while (cin >> type, type != '*'){
shape_lib[i].set(type);
++i;
}
size = i;
point_counter = 1;
if (is_first){
is_first = false;
}
else{
cout << endl;
}
while (cin >> point_x >> point_y, point_x != END_NUMBER && point_y != END_NUMBER){
has_inside = false;
for (i = 0; i != size; ++i){
if (shape_lib[i].is_inside(point_x, point_y)){
cout << "Point " << point_counter << " is contained in figure " << i + 1 << endl;
has_inside = true;
}
}
if (!has_inside){
cout << "Point " << point_counter << " is not contained in any figure" << endl;
}
++point_counter;
}
}
return 0;
}