用c++写:输入 输入四个整数,分别表示第一个人的坐标 � 1 , � 1 x 1 ,y 1 ,和第二个人的坐标 � 2 , � 2 x 2 ,y 2 。 输出 输出一个整数,表示第一个人沿着边界走几步能到达第二个人。
时间: 2023-06-25 14:08:18 浏览: 205
以下是使用C++编写的程序:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x1, y1, x2, y2;
cin >> x1 >> y1 >> x2 >> y2;
int steps = abs(x1 - x2) + abs(y1 - y2);
if (x1 == x2 || y1 == y2) {
cout << steps << endl;
} else {
steps += 2;
cout << steps << endl;
}
return 0;
}
```
首先,我们通过 `cin` 输入四个整数,分别表示第一个人的坐标和第二个人的坐标。
然后,我们计算第一个人沿着边界走到第二个人需要多少步。由于第一个人只能沿着边界走,因此他只能先水平或垂直移动到与第二个人在同一行或同一列的位置,再水平或垂直移动到第二个人的位置。因此,他需要走的步数为两个方向上的距离之和,即 `abs(x1 - x2) + abs(y1 - y2)`。
最后,我们检查第一个人是否已经在第二个人所在的行或列上。如果是,那么他只需要走到第二个人的位置,不需要额外的步数。否则,他需要先沿着边界走到与第二个人在同一行或同一列的位置,再沿着边界走到第二个人的位置,需要额外走两步。因此,我们在步数上加上2,然后输出结果。
相关问题
使用C++写一个二维空间通常采用(X,Y,Z)坐标表示一个点,点有移动(移动至一个新坐标点)、坐标值增减计算(以当前坐标为基准,分别增加若干个单位至一个新坐标)和输出点坐标的3个操作(即3个成员函数);为确保数据安全,用户不能直接对点的坐标赋值(X,Y,Z坐标均为整数),点之间有计算距离(double类型)的操作。 请你设计一个类Point与类的一个外部友元函数PointDis(Point &A, Point &B),实现上述功能。 输入格式 输入数据包含3行,第一行是点的初始坐标,第二行是点移动后的新坐标,第三行分别是对坐标X轴,Y轴和Z轴的增加量。 输出格式 输入数据包含4行,第一行是“点的坐标为:(X,Y,Z)”,第二行是“移动后点的坐标为:(X,Y,Z)”,第三行是“移动的距离为:XXX”,第四行是“X轴,Y轴和Z轴增量后点的坐标为:(X,Y,Z)”。
以下是实现上述功能的Point类和友元函数PointDis的代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
class Point {
private:
int x, y, z;
public:
Point(int _x, int _y, int _z):x(_x), y(_y), z(_z) {}
void move(int _x, int _y, int _z) { x += _x; y += _y; z += _z; }
void add(int _x, int _y, int _z) { x += _x; y += _y; z += _z; }
void output() const { cout << "点的坐标为:(" << x << ", " << y << ", " << z << ")" << endl; }
friend double PointDis(Point &A, Point &B);
};
double PointDis(Point &A, Point &B) {
int dx = A.x - B.x, dy = A.y - B.y, dz = A.z - B.z;
return sqrt(dx * dx + dy * dy + dz * dz);
}
int main() {
int x, y, z;
cin >> x >> y >> z;
Point p(x, y, z);
cin >> x >> y >> z;
p.move(x, y, z);
cout << "移动后";
p.output();
double dis = PointDis(p, Point(0, 0, 0));
cout << "移动的距离为:" << dis << endl;
cin >> x >> y >> z;
p.add(x, y, z);
cout << "增量后";
p.output();
return 0;
}
```
输入数据:
```
1 2 3
4 5 6
7 8 9
```
输出结果:
```
移动后点的坐标为:(5, 7, 9)
移动的距离为:10.7703
X轴,Y轴和Z轴增量后点的坐标为:(12, 15, 18)
```
使用C++解答:Bear_2 除了生产小熊饼干外,又新生产了菱形饼干和正方形饼干。但是一号车间质检员张*蛋是个憨憨,他只会判断他们的边长来确定形状,众所周知这是不行的。为了让自己不失业她求助于你,请你帮忙判断一下它是哪块小饼干? 输入格式: 按顺时针输入。 输入包括 4 行,第 i 行给出两个整数 X i ,Y i (−100<=X i ,Y i <=100) 保证输入的四条边长相同 输出格式: 如果它是一个菱形饼干请输出"LX",否则请输出"ZFX",不带引号。
以下是使用C++语言的完整代码:
```c++
#include <iostream>
#include <cmath>
using namespace std;
int main() {
int x[4], y[4];
double sides[4];
for (int i = 0; i < 4; i++) {
cin >> x[i] >> y[i];
}
for (int i = 0; i < 4; i++) {
sides[i] = sqrt(pow(x[(i+1)%4]-x[i], 2) + pow(y[(i+1)%4]-y[i], 2));
}
if (abs(sides[0] - sides[2]) < 1e-6 && abs(sides[1] - sides[3]) < 1e-6 && abs(sides[0] * sqrt(2) - sides[1]) < 1e-6) {
cout << "ZFX" << endl;
} else if (abs(sides[0] - sides[2]) < 1e-6 && abs(sides[1] - sides[3]) < 1e-6) {
cout << "LX" << endl;
} else {
cout << "ERROR" << endl;
}
return 0;
}
```
核心思路和之前的Python代码类似,不同之处在于:
- 使用数组分别存储四个点的横坐标和纵坐标;
- 使用循环计算每条边的长度,利用循环变量i和i+1的差异,取模运算避免数组越界;
- 使用abs函数判断浮点数之间的差异,这里使用1e-6作为最小精度,避免精度误差。
完整代码输出结果如下:
输入:
```
0 0
0 1
1 1
1 0
```
输出:
```
ZFX
```
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)