本题是代码填空题,按要求补充给出代码的空白即可通过题目。
#include<bits/stdc++.h>
using namespace std;
const int N = 205;
int n, m;
struct Point{
int x, y;
double det(){
return sqrt((double)x * x + y * y);
}
Point operator+(/*补全函数*/){
/*补全函数*/
}
} p[N];
Point operator-(/*补全函数*/){
/*补全函数*/
}
/*
补全其余未定义的函数
*/
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &p[i].x, &p[i].y);
for(int i = 1; i <= m; ++i){
int op, r, s;
scanf("%d%d", &op, &r);
if(op != 1)
scanf("%d", &s);
if(op == 1){
printf("%.2lf\n", p[r].det());
}
else if(op == 2){
Point t = p[r] + p[s];
printf("%d %d\n", t.x, t.y);
}
else if(op == 3){
Point t = p[r] - p[s];
printf("%d %d\n", t.x, t.y);
}
else if(op == 4){
printf("%d\n", dot(p[r], p[s]));
}
else if(op == 5){
printf("%d\n", cross(p[r], p[s]));
}
else if(op == 6){
printf("%.2lf\n", dis(p[r], p[s]));
}
}
return 0;
}
先输入 n 和 m,再输入 n 个二维坐标(编号从 1 开始),接着输入 m 个询问,对其中每个询问做出一个回答。 询问包括:
1 r 求第 r 个坐标对应的向量的模长(模长公式 xr2+yr2)。 2 r s 求第 r 个坐标对应的向量和第 s 个坐标对应的向量的加法结果(加法公式 (xr,yr)+(xs,ys)=(xr+xs,yr+ys))。3 r s 求第 r 个坐标对应的向量和第 s 个坐标对应的向量的减法结果(减法公式 (xr,yr)−(xs,ys)=(xr−xs,yr−ys))。4 r s 求第 r 个坐标对应的向量和第 s 个坐标对应的向量的点乘结果(点乘公式 (xr,yr)⋅(xs,ys)=xrxs+yrys)。5 r s 求第 r 个坐标对应的向量和第 s 个坐标对应的向量的叉乘结果(叉乘公式 (xr,yr)×(xs,ys)=xrys−yrxs)。6 r s 求点 r 和点 s 之间的距离(距离公式 (xr−xs)2+(yr−ys)2)。从标准输入读入数据。 第一行输入两个正整数 n,m(n,m≤100)。 接下来 n 行每行输入两个整数 x,y(−1000≤x,y≤1000),表示两个坐标。 接下来 m 行每行输入一个询问,格式见题面。
输出到标准输出。 对于每个询问,回答一行。 如果答案是一个数量,直接输出;如果答案是一个二维向量(或二维坐标),按先 x 后 y 的顺序输出该坐标。 对于第 1 类和第 6 类询问输出保留 2 位小数。
3 6
1 1
2 3
3 2
1 2
2 1 2
3 1 2
4 1 2
5 1 2
6 2 3
3.61
3 4
-1 -2
5
1
1.41
代码:
#include<bits/stdc++.h>
using namespace std;
const int N = 205;
int n, m;
struct Point{
int x, y;
double det(){
return sqrt((double)x * x + y * y);
}
Point operator%(Point &b){
return (Point){sqrt(b.x/2 + b.y/2)};
}
} p[N];
Point operator^(Point &a,Point &b){
return (Point){a.x + b.x,a.y + b.y};
}
Point dot(Point &a ,Point &b){
return(Point) {a.x*b.x + a.y*b.y};
}
Point cross(Point &a,Point &b){
return(Point) {abs(a.x*b.x - a.y*b.y)};
}
Point dis(Point &a,Point &b){
return(Point){sqrt((double)pow(a.x-b.x,2)+ pow(a.y - b.y,2))};
}
int main() {
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; ++i)
scanf("%d%d", &p[i].x, &p[i].y);
for(int i = 1; i <= m; ++i){
int op, r, s;
scanf("%d%d", &op, &r);
if(op != 1)
scanf("%d", &s);
if(op == 1){
printf("%.2lf\n", p[r].det());
}
else if(op == 2){
Point t = p[r] % p[s];
printf("%d %d\n", t.x, t.y);
}
else if(op == 3){
Point t = p[r] ^ p[s];
printf("%d %d\n", t.x, t.y);
}
else if(op == 4){
printf("%d\n", dot(p[r], p[s]));
}
else if(op == 5){
printf("%d\n", cross(p[r], p[s]));
}
else if(op == 6){
printf("%.2lf\n", dis(p[r], p[s]));
}
}
return 0;
}