C++ 需要找到构成最大周长的3个点
我有一个叫做point的结构。显然,它提供了点(x和y)的坐标。我有一系列的要点C++ 需要找到构成最大周长的3个点,c++,C++,我有一个叫做point的结构。显然,它提供了点(x和y)的坐标。我有一系列的要点 我需要做的是找到3个点,组成最大的三角形。我尝试了很多选择,但都没有成功。 你能想出一个算法来产生我需要的结果吗 #include "stdafx.h" #include <iostream> #include <vector> using namespace std; struct point { double x, y; }; int main() { do
我需要做的是找到3个点,组成最大的三角形。我尝试了很多选择,但都没有成功。 你能想出一个算法来产生我需要的结果吗
#include "stdafx.h"
#include <iostream>
#include <vector>
using namespace std;
struct point {
double x, y;
};
int main()
{
double n, c1, c2;
double max(0), max1(0), max2(0), temp;
point *a = NULL;
cout << "Enter the number of points: ";
cin >> n;
a = new point[n];
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a[i] = dot;
};
for (int i = 0; i < n-1; i++) { // here I'm running out of ideas
for (int j = 1; j < n; j++) {
temp = sqrt((a[i].x + a[i].y)*(a[i].x + a[i].y)- (a[j].x + a[j].y)*(a[j].x + a[j].y));
if (temp > max)
}
}
#包括“stdafx.h”
#包括
#包括
使用名称空间std;
结构点{
双x,y;
};
int main()
{
双n,c1,c2;
双最大值(0)、最大值1(0)、最大值2(0)、温度;
点*a=NULL;
cout>n;
a=新点[n];
对于(int i=0;i>c1>>c2;
点-点;
点x=c1;
点y=c2;
a[i]=点;
};
对于(inti=0;i最大值)
}
}
您可以迭代所有三个点的集合。请注意,最好使用向量而不是数组,将代码放在单独的函数中计算周长是很有用的
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
struct point {
double x, y;
};
double perim(point p1, point p2, point p3)
{
double result = 0;
result += sqrt(pow(p1.x - p2.x, 2) + pow(p1.y - p2.y, 2));
result += sqrt(pow(p2.x - p3.x, 2) + pow(p2.y - p3.y, 2));
result += sqrt(pow(p3.x - p1.x, 2) + pow(p3.y - p1.y, 2));
return result;
}
int main()
{
double n, c1, c2;
double max(0), temp;
int p1 = 0, p2 = 0, p3 = 0;
vector <point> a;
cout << "Enter the number of points: ";
cin >> n;
for (int i = 0; i < n; i++) {
cin >> c1 >> c2;
point dot;
dot.x = c1;
dot.y = c2;
a.push_back(dot);
};
for (int i = 0; i < n - 2; i++) { // here I'm running out of ideas
for (int j = i+1; j < n - 1; j++) {
for (int k = j+1; k < n; k++) {
temp = perim(a[i], a[j], a[k]);
if (temp > max) {
max = temp;
p1 = i; p2 = j; p3 = k;
}
}
}
}
cout << p1 << " "<<p2<< " "<<p3;
}
#包括
#包括
#包括
使用名称空间std;
结构点{
双x,y;
};
双边界(点p1、点p2、点p3)
{
双结果=0;
结果+=sqrt(pow(p1.x-p2.x,2)+pow(p1.y-p2.y,2));
结果+=sqrt(pow(p2.x-p3.x,2)+pow(p2.y-p3.y,2));
结果+=sqrt(pow(p3.x-p1.x,2)+pow(p3.y-p1.y,2));
返回结果;
}
int main()
{
双n,c1,c2;
双最大值(0),温度;
int p1=0,p2=0,p3=0;
载体a;
cout>n;
对于(int i=0;i>c1>>c2;
点-点;
点x=c1;
点y=c2;
a、 向后推(点);
};
对于(inti=0;i最大值){
最大值=温度;
p1=i;p2=j;p3=k;
}
}
}
}
测试所有三元组的点不是很简单吗?你有#include
,所以用它来代替手动内存分配。有一点修正:循环应该是这样的:for(int i=0;i双距离(点p1,点p2)
。