四点c程序的最近对
我需要找到最接近的一对四点C程序。这个代码有三点。我需要这个解决方案来解决四个问题 我试过这个。此解决方案适用于三种输入 当我输入三个点时,我将得到最近的点,但我需要四个点中的最近点四点c程序的最近对,c,C,我需要找到最接近的一对四点C程序。这个代码有三点。我需要这个解决方案来解决四个问题 我试过这个。此解决方案适用于三种输入 当我输入三个点时,我将得到最近的点,但我需要四个点中的最近点 #include <stdio.h> #include <stdlib.h> #include <math.h> struct Point { int x, y ; }; double getDistanceAB(struct Point a, struct Point
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
struct Point
{
int x, y ;
};
double getDistanceAB(struct Point a, struct Point b)
{
double distanceAB;
distanceAB = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distanceAB;
}
double getDistanceBC(struct Point b, struct Point c)
{
double distanceBC;
distanceBC = sqrt((b.x - c.x) * (b.x - c.x) + (b.y-c.y) *(b.y-c.y));
return distanceBC;
}
double getDistanceAC(struct Point a, struct Point c)
{
double distanceAC;
distanceAC = sqrt((a.x - c.x) * (a.x - c.x) + (a.y-c.y) *(a.y-c.y));
return distanceAC;
}
int main()
{
struct Point a, b, c;
printf("Enter coordinate of points a: ");
scanf("%d %d", &a.x, &a.y);
printf("Enter coordinate of points b: ");
scanf("%d %d", &b.x, &b.y);
printf("Enter coordinate of points c: ");
scanf("%d %d", &c.x, &c.y);
if((getDistanceAB(a,b))>(getDistanceBC(b,c)) && (getDistanceAB(a,b))>(getDistanceBC(a,c)))
{
printf("Point A and B are closest.");
}
else if((getDistanceBC(b,c))>(getDistanceAC(a,c)) && (getDistanceBC(b,c))>(getDistanceAC(a,b)))
{
printf("Point B and C are closest.");
}
else if((getDistanceBC(a,c))>(getDistanceAC(a,b)) && (getDistanceBC(a,c))>(getDistanceAC(b,c)))
{
printf("Point A and C are closest.");
}
else
{
printf("All point are same.");
}
}
#包括
#包括
#包括
结构点
{
int x,y;
};
双getDistanceAB(结构点a、结构点b)
{
双距离;
距离ab=sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
返回距离;
}
双getDistanceBC(结构点b、结构点c)
{
双距离bc;
距离bc=sqrt((b.x-c.x)*(b.x-c.x)+(b.y-c.y)*(b.y-c.y));
返回距离b;
}
双getDistanceAC(结构点a、结构点c)
{
双距离;
距离ac=sqrt((a.x-c.x)*(a.x-c.x)+(a.y-c.y)*(a.y-c.y));
返回距离;
}
int main()
{
结构点a、b、c;
printf(“输入点a的坐标:”);
scanf(“%d%d”、“a.x”、“a.y”);
printf(“输入点b的坐标:”);
scanf(“%d%d”、“b.x”、“b.y”);
printf(“输入点c的坐标:”);
scanf(“%d%d”、&c.x和&c.y);
如果((getDistanceAB(a,b))>(getDistanceBC(b,c))&&(getDistanceAB(a,b))>(getDistanceBC(a,c)))
{
printf(“A点和B点最接近”);
}
否则如果((getDistanceBC(b,c))>(getDistanceAC(a,c))&(getDistanceBC(b,c))>(getDistanceAC(a,b)))
{
printf(“B点和C点最接近”);
}
否则如果((getDistanceBC(a,c))>(getDistanceAC(a,b))&(getDistanceBC(a,c))>(getDistanceAC(b,c)))
{
printf(“A点和C点最接近”);
}
其他的
{
printf(“所有点都相同”);
}
}
我会将函数的数量减少到两倍的getDistance(结构点p,结构点o)
并将您的点保存在列表中,这样您就可以允许程序动态地运行这些点,而不是对每个条件进行编程
一旦你的点在一个列表中,你可以运行一个循环,检查列表中每一对的距离,并对照当前最短的距离进行检查;如果选中对的距离更近,则更改当前到选中对的最短距离以及哪对点具有该距离
通过这种方式,您可以将其扩展为任意多个点
我不习惯C的语法,但是为了检查列表中的点,需要一个双for循环,其中第一个循环遍历列表中的每个点,第二个循环检查从/到第一个点到列表后面所有点的距离
for i = 0, i++, length(listOfPoints) {
for j = i+1, j++, length(listOfPoints) {
getDistance(listOfPoints[i], listOfPoints[j]
}
}
希望这能有所帮助。首先,改变这一点:
double getDistanceAB(struct Point a, struct Point b)
{
double distanceAB;
distanceAB = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) *(a.y-b.y));
return distanceAB;
}
double getDistanceBC(struct Point b, struct Point c)
{
double distanceBC;
distanceBC = sqrt((b.x - c.x) * (b.x - c.x) + (b.y-c.y) *(b.y-c.y));
return distanceBC;
}
double getDistanceAC(struct Point a, struct Point c)
{
double distanceAC;
distanceAC = sqrt((a.x - c.x) * (a.x - c.x) + (a.y-c.y) *(a.y-c.y));
return distanceAC;
}
为此:
double getDistance(struct Point a, struct Point b)
{
double distance;
distance = sqrt((a.x - b.x) * (a.x - b.x) + (a.y-b.y) * (a.y-b.y));
return distance;
}
函数的一个要点是不必重复代码
现在,您所要做的就是创建四个点,为第四个点再添加一次扫描,并将其添加到决策树中
请记住这一点,以便您制定决策树。。。如果您使用与您在原始帖子中使用的相同逻辑检查点“a”是否最接近,则无需再次比较点“a”。这里是任意点数的解决方案 只需将MAX_点更改为您可能需要的任何内容
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <stdbool.h>
#define MAX_POINTS (4U)
struct Point
{
int x;
int y;
};
struct PointPair
{
struct Point a;
struct Point b;
};
double getDistance(const struct PointPair pair)
{
return sqrt((pair.a.x - pair.b.x) * (pair.a.x - pair.b.x) +
(pair.a.y - pair.b.y) * (pair.a.y - pair.b.y));
}
void readPoints(struct Point points[const])
{
for (unsigned i = 0; i < MAX_POINTS; i++)
{
printf("Enter coordinate of point %u: ", i);
scanf("%d %d", &(points[i].x), &(points[i].y));
}
}
bool checkForShorterDistance(const struct PointPair pair, double *const p_minDistance)
{
double tempDistance = getDistance(pair);
if (tempDistance < *p_minDistance)
{
*p_minDistance = tempDistance;
return true;
}
return false;
}
struct PointPair getClosestPair(const struct Point points[const])
{
struct PointPair result =
{
.a = points[0],
.b = points[1]
};
double minDistance = getDistance(result);
struct PointPair tempPair;
unsigned i, j;
for (i = 0; i < MAX_POINTS; i++)
{
tempPair.a = points[i];
for (j = 0; j < MAX_POINTS; j++)
{
if (i == j)
{
continue;
}
tempPair.b = points[j];
if (checkForShorterDistance(tempPair, &minDistance))
{
result = tempPair;
}
}
}
return result;
}
int main(void)
{
struct Point points[MAX_POINTS];
readPoints(points);
struct PointPair pair = getClosestPair(points);
printf("Closest pair is (%d, %d) and (%d, %d)\n",
pair.a.x,
pair.a.y,
pair.b.x,
pair.b.y);
return 0;
}
#包括
#包括
#包括
#包括
#定义最大点(4U)
结构点
{
int x;
int-y;
};
结构点对
{
结构点a;
结构点b;
};
双getDistance(常量结构点对)
{
返回sqrt((pair.a.x-pair.b.x)*(pair.a.x-pair.b.x)+
(配对a.y-配对b.y)*(配对a.y-配对b.y));
}
无效读取点(结构点点[const])
{
对于(无符号i=0;i
我就是这样解决的
#include <stdio.h>
typedef struct
{
int x;
int y;
} Point;
int square(int x) { return x * x; }
int distanceSq(Point *a, Point *b)
{
return square(a->x - b->x) + square(a->y - b->y);
}
int main(int argc, char const *argv[])
{
int n = 4;
Point a[4];
for (int i = 0; i < n; i++)
{
printf("Enter Point %d <as x y>: ", i + 1);
scanf("%d %d", &a[i].x, &a[i].y);
}
int distance = __INT_MAX__;
int p1 = -1, p2 = -1;
for (int i = 0; i < n - 1; i++)
for (int j = i + 1; j < n; j++)
{
int current = distanceSq(&a[i], &a[j]);
if (current < distance)
{
p1 = i;
p2 = j;
distance = current;
}
}
printf("The closest points are [%d %d] and [%d %d]", a[p1].x, a[p1].y, a[p2].x, a[p2].y);
return 0;
}
#包括
类型定义结构
{
int x;
int-y;
}点;
整数平方(整数x){返回x*x;}
内部距离SQ(点*a,点*b)
{
返回方块(a->x-b->x)+方块(a->y-b->y);
}
int main(int argc,char const*argv[]
{
int n=4;
a点[4];
对于(int i=0;i
注: