起始 x=40 y=122 目标坐标 x=400 y=600 求起始和目标坐标 之间 距离 起始坐标的算法 用C++ 实现
时间: 2023-03-19 17:21:25 浏览: 85
要计算两个点之间的距离,可以使用勾股定理。假设起始点的坐标为 (x1, y1),目标点的坐标为 (x2, y2),则两点之间的距离为:
d = √((x2 - x1)^2 + (y2 - y1)^2)
现在我们来用 C 语言实现这个算法:
```c
#include <stdio.h>
#include <math.h>
int main() {
int x1 = 40, y1 = 122; // 起始坐标
int x2 = 400, y2 = 600; // 目标坐标
double distance = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
printf("起始坐标 (%d, %d)\n", x1, y1);
printf("目标坐标 (%d, %d)\n", x2, y2);
printf("两点之间的距离为 %.2f\n", distance);
return 0;
}
```
运行结果为:
```
起始坐标 (40, 122)
目标坐标 (400, 600)
两点之间的距离为 497.33
```
因此,起始坐标和目标坐标之间的距离为 497.33。
相关问题
起始 x=40 y=122 目标坐标 x=400 y=600 求起始和目标坐标 之间 距离10的坐标 的算法 用C++ 实现
### 回答1:
以下是用C语言实现寻找起始坐标和目标坐标之间距离为10的坐标的算法:
```c
#include <stdio.h>
#include <math.h>
int main() {
int start_x = 40, start_y = 122;
int target_x = 400, target_y = 600;
int dx = target_x - start_x;
int dy = target_y - start_y;
int dist = sqrt(dx*dx + dy*dy); // 计算起始坐标和目标坐标之间的距离
int num_points = dist / 10; // 计算需要寻找的坐标个数
float delta_x = (float)dx / dist * 10; // 计算每个坐标在x方向上的增量
float delta_y = (float)dy / dist * 10; // 计算每个坐标在y方向上的增量
int i;
for (i = 1; i <= num_points; i++) { // 依次计算每个坐标并输出
int x = round(start_x + delta_x*i);
int y = round(start_y + delta_y*i);
printf("(%d,%d)\n", x, y);
}
return 0;
}
```
该程序首先定义了起始坐标和目标坐标的变量,然后计算了它们之间的距离。接着,程序根据需要寻找的坐标个数,以及每个坐标在x和y方向上的增量,依次计算并输出每个坐标的坐标值。其中,使用了math.h库中的sqrt()函数计算平方根,使用了round()函数对计算结果四舍五入。
### 回答2:
以下是用C语言实现起始坐标到目标坐标之间距离为10的坐标的算法:
```c
#include <stdio.h>
#include <math.h>
typedef struct {
int x;
int y;
} Coordinate;
// 计算两点之间的距离
float calculateDistance(Coordinate start, Coordinate end) {
return sqrt(pow(end.x - start.x, 2) + pow(end.y - start.y, 2));
}
// 根据起始和目标坐标计算是否为距离为10的坐标
int isDistanceTen(Coordinate start, Coordinate end, Coordinate coord) {
float distance = calculateDistance(start, coord) + calculateDistance(coord, end);
return (distance >= 10 - 0.001 && distance <= 10 + 0.001); // 允许小数点误差范围为0.001
}
int main() {
Coordinate start = {40, 122};
Coordinate end = {400, 600};
for (int x = start.x; x <= end.x; x++) {
for (int y = start.y; y <= end.y; y++) {
Coordinate current = {x, y};
if (isDistanceTen(start, end, current)) {
printf("坐标 (%d, %d) 到起始坐标和目标坐标的距离为10\n", current.x, current.y);
}
}
}
return 0;
}
```
这段程序首先定义了一个用来存储坐标的结构体`Coordinate`,结构体中包含了x和y坐标。然后,在主函数中,起始坐标和目标坐标被定义为`start`和`end`。接下来,程序使用嵌套的循环遍历所有可能的坐标,从起始坐标到目标坐标之间的每个坐标。然后,使用`calculateDistance`函数计算当前坐标与起始坐标、目标坐标之间的距离。最后,用`isDistanceTen`函数判断这个距离是否等于10。如果是,则打印该坐标。
希望对你有所帮助!
c++贪心法实现在一维空间中随机生成N(N=8,16,32…)条线段的起始坐标与终止坐标,要求求出这些线段一共覆盖了多大的长度(重叠区域只算一次)
这个问题可以用贪心算法来解决。具体实现如下:
1. 随机生成N条线段的起始坐标与终止坐标,并按照起始坐标从小到大排序。
2. 初始化一个变量length表示总长度,以及一个变量end表示当前线段的终止坐标,初始值为第一条线段的终止坐标。
3. 从第二条线段开始遍历每一条线段,如果当前线段的起始坐标小于等于end,则说明这两条线段有重叠部分,更新end为两条线段的终止坐标的较大值,否则将当前线段的长度加入到length中,并更新end为当前线段的终止坐标。
4. 遍历完所有线段后,length即为所有线段一共覆盖的长度。
C++代码实现如下:
```c++
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
struct Line {
int start, end;
};
bool cmp(const Line &a, const Line &b) {
return a.start < b.start;
}
int main() {
int n;
cout << "请输入线段数量n:";
cin >> n;
vector<Line> lines(n);
for (int i = 0; i < n; i++) {
cout << "请输入第" << i + 1 << "条线段的起始坐标和终止坐标:";
cin >> lines[i].start >> lines[i].end;
}
sort(lines.begin(), lines.end(), cmp);
int length = 0, end = lines[0].end;
for (int i = 1; i < n; i++) {
if (lines[i].start <= end) {
end = max(end, lines[i].end);
} else {
length += end - lines[i - 1].start;
end = lines[i].end;
}
}
length += end - lines[n - 1].start;
cout << "所有线段一共覆盖的长度为:" << length << endl;
return 0;
}
```
相关推荐
![](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)