点在哪里
来源:互联网 发布:java开发中间件 编辑:程序博客网 时间:2024/06/10 02:55
题目描述:
输出一组图形(矩形或圆)和一组点的数据,判断点的位置。
输入格式:
输入一组图形的数据,其中每行以“c”开头表示圆,以“r”开头表示矩形,其中矩形是依次给出左下脚和右上角坐标,圆是给出圆心坐标及半径,图形数据以输入另起一行的“*”结束,接下来是给出点的坐标(x,y),整个输入文件以9999.9 9999.9结束,每行中的各个数据以空格分隔。
输出格式:
输出点的位置如下信息:直接输出到屏幕上,如某点在图形上,则输出所有图形上的信息:Point i is conatined in figure j;如果点不在任一图形内,则输出:Point i is not contained in any figure。
输入输出样例:
Sample input
Output for the input
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
Point 1 is contained in figure 1
Point 2 is contained in figure 1
Point 2 is contained in figure 3
Sample input
Output for the input
r 8.5 -8.5 25.5 17.0
c 20.2 7.3 5.8
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
c 5.0 15.0 7.2
*
6.9 11.2
20.0 20.0
17.6 3.2
-5.2 -7.8
4.7 5.3
2.0 2.0
9999.9 9999.9
Point 1 is contained in figure 5
Point 1 is contained in figure 6
Point 2 is not contained in any figure
Point 3 is contained in figure 1
Point 3 is contained in figure 2
Point 4 is contained in figure 4
Point 5 is contained in figure 3
Point 5 is contained in figure 5
Point 6 is contained in figure 3
解:
如果点(x,y)在矩形(x1,y1),(x2,y2)内,一定满足条件:x1<=x<=x2 && y1<=y<=y2
如果点(x,y)在圆(x1,y1,r)内,一定满足条件:sqrt((x-x1)2+(y-y1)2)<=r
.
代码:
#include<math.h>
#include<string.h>
#include<stdlib.h>
#define MAXLEN 1000
#define RGET(TYPE,VAR,LOC) ((TRectangle *)TYPE[LOC].data)->VAR
#define CGET(TYPE,VAR,LOC) ((TCircle *)TYPE[LOC].data)->VAR
struct TRectangle...{ //矩形
float x1,x2,y1,y2;
};
struct TCircle...{ //圆
float x,y,r;
};
class Figure...{
public:
void *data;
char type; //type='r' 表示矩形;type='c' 表示圆
};
int figlen;
Figure figure[MAXLEN];
inline void
swap(float &a,float &b)...{ //交换a和b的值
float c;
c=a;
a=b;
b=c;
}
inline bool
equal(const float a,const float b)...{ //判断俩浮点数是否相等
return fabs(a-b)<1e-6;
}
inline void
readRecord()...{ //读取图形数据
char type;
while(1)...{
type=getchar();
if(type=='*') return;
figure[figlen].type=type;
if(type=='r')...{
figure[figlen].data=new TRectangle;
scanf("%f%f%f%f",&RGET(figure,x1,figlen),&RGET(figure,y1,figlen),&RGET(figure,x2,figlen),&RGET(figure,y2,figlen));
figlen++;
}
else if(type=='c')...{
figure[figlen].data=new TCircle;
scanf("%f%f%f",&CGET(figure,x,figlen),&CGET(figure,y,figlen),&CGET(figure,r,figlen));
figlen++;
}
}
}
inline void
caculate()...{ //输入点并输出所在图形
float a,b,c,d;
int i,no=0;
bool ok;
while(1)...{
scanf("%f%f",&a,&b);
if(equal(a,9999.9f) && equal(b,9999.9f)) return;
++no;
ok=false;
for(i=0;i<figlen;i++)
if(figure[i].type=='r')...{
if(RGET(figure,x1,i)<a && RGET(figure,x2,i)>a && RGET(figure,y1,i)<b && RGET(figure,y2,i)>b)...{
printf("Point %d is contained in figure %d ",no,i+1);
ok=true;
}
}
else...{
c=CGET(figure,x,i)-a;
c*=c;
d=CGET(figure,y,i)-b;
d*=d;
if(sqrt(c+d)<CGET(figure,r,i) || equal((float)sqrt(c+d),CGET(figure,r,i)))...{
printf("Point %d is contained in figure %d ",no,i+1);
ok=true;
}
}
if(ok==false) printf("Point %d is not contained in any figure ",no);
}
}
void
main()...{
int fd;
fd=open("test.txt",O_RDONLY);
dup2(fd,0);
fd=open("result.txt",O_TRUNC|O_WRONLY);
dup2(fd,1);
figlen=0;
readRecord();
caculate();
}
- 点在哪里
- 可执行程序的入口点在哪里?
- 不知道在哪里下载的点我!
- 传统RAID的性能瓶颈点在哪里?
- 大数据漫谈2:大数据价值点在哪里
- 产品背后:你产品的记忆点在哪里?
- 在哪里?
- 《在哪里》
- 在哪里?
- 在哪里?
- 步步高打火机,哪里不会点哪里
- 鼠标点哪里,哪里出特效
- [技术分享 – RMS 篇] RMS 的服务连接点在哪里?
- c++中CMainframe和CView类的区别?C++工程中的主入口点在哪里?
- 自定义虚拟键盘-光标不随自己点在哪里而移动
- 椭圆上一个动点P.问P在哪里时∠F1PF2最大的巧妙想法
- 悟空在哪里?奶妈在哪里?
- 全局变量在哪里声明,在哪里定义
- ORACLE 程序包 dbms_sql
- 网页中运行其它html代码
- Python Challenge 谜题0
- div+javascript实现popup
- Python Challenge 谜题1
- 点在哪里
- 重写在多态中的应用
- C++常量成员函数特殊说明
- EXCEL文件里的求和值都变成公式了,我在[格式]--[单元格]里已将其调整为数值,但还是不行
- 关于LINUX 磁盘阵列的挂载问题?
- Python Challenge 谜题2
- strcpy/memcpy几个字符串函数的完美版(原)
- C/C++笔试、面试题目大汇总
- JSTL 1.1 简介