C语言输入正整数坐标)x.y
时间: 2023-05-22 07:04:20 浏览: 88
你可以使用原生的 scanf 函数来输入正整数坐标 x 和 y,并进行错误检查,以下是示例代码:
```c
int x, y;
do {
printf("请输入正整数坐标(x.y): ");
} while (scanf("%d.%d", &x, &y) != 2 || x <= 0 || y <= 0);
printf("坐标为 (%d, %d)\n", x, y);
```
该代码会反复提醒用户输入,直到输入格式正确且坐标为正整数。
相关问题
C语言输入正整数坐标点(x,y),判断落在哪个象限中
。这个问题很简单,可以用以下代码实现:
```c
#include <stdio.h>
int main()
{
int x, y;
printf("请输入坐标点(x, y):");
scanf("%d,%d", &x, &y);
if (x > 0 && y > 0) {
printf("(%d, %d) 落在第一象限\n", x, y);
} else if (x < 0 && y > 0) {
printf("(%d, %d) 落在第二象限\n", x, y);
} else if (x < 0 && y < 0) {
printf("(%d, %d) 落在第三象限\n", x, y);
} else if (x > 0 && y < 0) {
printf("(%d, %d) 落在第四象限\n", x, y);
} else {
printf("(%d, %d) 不属于任何一个象限\n", x, y);
}
return 0;
}
```
c语言写 设是平面上散列的个点的集合。请编写程序找出集合中距离最近的点对的距离。 输入格式: 输入第一行给出一个正整数,表示平面上点的数量。随后行,每行给出一个实数对,每个实数对表示一个点的坐标值,其中第1数表示横坐标,第2数表示纵坐标。 输出格式: 最近点对中两个点之间的距离。 输入样例: 5 -1.00 2.00 0.00 2.00 0.50 0.60 1.00 1.00 2.00 0.00 输出样例: 0.6403
下面是用 C 语言编写的代码实现:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define MAXN 10000 // 最大点数
typedef struct {
double x, y;
} Point;
int cmp_x(const void* a, const void* b)
{
Point* p1 = (Point*) a;
Point* p2 = (Point*) b;
return p1->x - p2->x;
}
int cmp_y(const void* a, const void* b)
{
Point* p1 = (Point*) a;
Point* p2 = (Point*) b;
return p1->y - p2->y;
}
double distance(Point* p1, Point* p2)
{
double dx = p1->x - p2->x;
double dy = p1->y - p2->y;
return sqrt(dx * dx + dy * dy);
}
double closest_pair(Point* points, int n)
{
if (n <= 1) {
return 1e20; // 如果只有1个点,返回一个极大值
}
int m = n / 2;
double x = points[m].x;
double d = fmin(closest_pair(points, m), closest_pair(points + m, n - m));
// 按照 x 坐标将点集分成两个部分
qsort(points, n, sizeof(Point), cmp_y);
// 在分界线附近找到距离更小的点对
Point tmp[MAXN];
int k = 0;
for (int i = 0; i < n; i++) {
if (fabs(points[i].x - x) < d) {
for (int j = k - 1; j >= 0 && points[i].y - tmp[j].y < d; j--) {
double dis = distance(&points[i], &tmp[j]);
if (dis < d) {
d = dis;
}
}
tmp[k++] = points[i];
}
}
return d;
}
int main()
{
int n;
Point points[MAXN];
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%lf%lf", &points[i].x, &points[i].y);
}
qsort(points, n, sizeof(Point), cmp_x);
printf("%.4f\n", closest_pair(points, n));
return 0;
}
```
这个代码使用了分治法求解最近点对问题。首先按照 x 坐标将点集排序,然后将点集分成两个部分,递归地求解左半部分和右半部分的最近点对距离。然后将点集按照 y 坐标排序,找到中间分界线附近距离更小的点对。
具体来说,我们可以按照下面的步骤求解最近点对距离:
1. 如果点集中只有一个点,返回一个极大值。
2. 将点集按照 x 坐标排序,并将点集分成两个部分。
3. 递归地求解左半部分和右半部分的最近点对距离。
4. 计算最小距离 d,其中 d 是左半部分和右半部分的最近点对距离中的较小值。
5. 将点集按照 y 坐标排序,并在分界线附近找到距离更小的点对。
6. 返回最小距离 d。
在代码实现中,我们使用了 `qsort` 函数对点集进行排序,使用了 `fabs` 函数计算绝对值。在计算点对距离时,我们使用了 `sqrt` 函数求解平方根,使用了 `fmin` 函数求解两个数中的较小值。
需要注意的是,如果点集中有多个点的 x 坐标相同,我们需要在计算 y 坐标距离时,只考虑与当前点 y 坐标差不超过 d 的点,这样才能保证算法的正确性。为了实现这个功能,我们使用了一个辅助数组 `tmp`,存储与当前点 x 坐标距离不超过 d 的点,然后按照 y 坐标排序,找到距离更小的点对。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.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://img-home.csdnimg.cn/images/20210720083646.png)
![](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)