Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 计算直角三角形:如何提高时间复杂度?_C++_Optimization - Fatal编程技术网

C++ 计算直角三角形:如何提高时间复杂度?

C++ 计算直角三角形:如何提高时间复杂度?,c++,optimization,C++,Optimization,以下是我正在研究的问题: N个点放置在坐标平面中 编写一个程序,计算我们可以选择三种方法的数量 点,使它们形成一个直角三角形,腿平行于 坐标轴 直角三角形有一个90度的内角。直角三角形的两腿是它的两条较短的边 下面是我如何组织代码的 对于每一点,我都检查了另一点。如果两个点具有匹配的x坐标和不同的y坐标,我会仔细查看这些点,找到一个y坐标与新点相同、x坐标不同的点。如果找到了,我检查了直角斜边在三个点上是否正确 类似地,我对两个具有匹配y坐标和不同x坐标的点重复了修改 该程序可以工作,但超过了时

以下是我正在研究的问题:

N个点放置在坐标平面中

编写一个程序,计算我们可以选择三种方法的数量 点,使它们形成一个直角三角形,腿平行于 坐标轴

直角三角形有一个90度的内角。直角三角形的两腿是它的两条较短的边

下面是我如何组织代码的

对于每一点,我都检查了另一点。如果两个点具有匹配的x坐标和不同的y坐标,我会仔细查看这些点,找到一个y坐标与新点相同、x坐标不同的点。如果找到了,我检查了直角斜边在三个点上是否正确

类似地,我对两个具有匹配y坐标和不同x坐标的点重复了修改

该程序可以工作,但超过了时间复杂度,我不知道如何降低它

这是我的密码:

double distwithoutroot(int x1, int y1, int x2, int y2) {
    int xdist = pow((x2 - x1),2);
    int ydist = pow((y2 - y1),2);
    return  xdist + ydist;
}

int main() {
    int noofpoints;

    cin >> noofpoints;
    int xs[100000];
    int ys[100000];
    int count = 0;

    for (int i = 0; i < noofpoints; i++) {
        cin >> xs[i] >> ys[i];
    }

    for (int i = 0; i < noofpoints; i++) {
        int main_x_point = xs[i];
        int main_y_point = ys[i];

        for (int j = 0; j < noofpoints; j++) {
            int checkmatchx = xs[j];
            int checkmatchy = ys[j];

            if (main_x_point == checkmatchx && main_y_point != checkmatchy) {

                for (int k = 0; k < noofpoints; k++) {
                    int secondcheckx = xs[k];
                    int secondchecky = ys[k];

                    if (checkmatchy == secondchecky && checkmatchx != secondcheckx) {
                        int hypotenus = distwithoutroot(main_x_point, main_y_point, secondcheckx, secondchecky);

                        int perpendicular = distwithoutroot(main_x_point, main_y_point, checkmatchx, checkmatchy);
                        int base = distwithoutroot(secondcheckx, secondchecky, checkmatchx, checkmatchy);
                        if (hypotenus== ( perpendicular+ base )) {
                            count += 1;
                            }
                        }
                }
            }

            else if (main_y_point == checkmatchy && main_x_point != checkmatchx) {
                for (int k = 0; k < noofpoints; k++) {
                    int secondcheckx = xs[k];
                    int secondchecky = ys[k];
                    if (checkmatchx == secondcheckx && checkmatchy != secondchecky) {
                        int hypotenus = distwithoutroot(main_x_point, main_y_point, secondcheckx, secondchecky);
                        int base = distwithoutroot(main_x_point, main_y_point, checkmatchx, checkmatchy);
                        int perpendicular = distwithoutroot(secondcheckx, secondchecky, checkmatchx, checkmatchy);
                        if (hypotenus == (perpendicular + base)) {
                            count += 1;
                        }
                    }
                }
            }


        }

        cout<<count;

    }
double distwithout root(int-x1、int-y1、int-x2、int-y2){
int-xdist=pow((x2-x1),2);
int ydist=功率((y2-y1),2);
返回xdist+ydist;
}
int main(){
整数点;
cin>>noofpoints;
int xs[100000];
国际货币基金组织[100000];
整数计数=0;
for(int i=0;i>xs[i]>>ys[i];
}
for(int i=0;icout你只需要把所有的点放在两张地图上,x和y。运行时间减少到O(N)*O(T),T是同一个角上三角形的最大值。

你只需要把所有的点放在两张地图上,x和y。运行时间减少到O(N)*O(T),T是同一个角上三角形的最大值。

把这个想法扩展到

请注意,不需要检查毕达哥拉斯定理。如果三角形有一个水平支腿和一个垂直支腿,则它是一个直角三角形。要计算此类三角形的数量,您只需要有X和Y坐标等于任何值的点数

因此,代码草图:

std::map<int, int> map_x; // for each X, counts the points for which xs = X
std::map<int, int> map_y; // for each Y, counts the points for which ys = Y

for (int i = 0; i < noofpoints; i++)
    ++map_x[xs[i]];

for (int i = 0; i < noofpoints; i++)
    ++map_x[ys[i]];

for (int i = 0; i < noofpoints; i++)
    count += (map_x[xs[i]] - 1) * (map_y[ys[i]] - 1);
std::map map_x;//对于每个x,计算x=x的点
std::map_y;//对于每个y,计算y=y的点
for(int i=0;i
关于最后一个循环的说明:

它迭代所有点,并计算该点位于直角位置的直角三角形的数量

对于每个点,它知道有多少点具有相同的X和多少点具有相同的Y(必须减去1才能排除相同的点)。这些点的每一个组合都给出一个合适的三角形

顺便说一句,有些三角形可能退化(面积为零);而且,如果有些点出现两次,有些三角形可能是相同的。我不认为你应该将它们排除在计数之外。

在中扩展这个概念

请注意,不需要检查毕达哥拉斯定理。如果三角形有一个水平支腿和一个垂直支腿,则它是一个直角三角形。要计算此类三角形的数量,您只需要有X和Y坐标等于任何值的点数

因此,代码草图:

std::map<int, int> map_x; // for each X, counts the points for which xs = X
std::map<int, int> map_y; // for each Y, counts the points for which ys = Y

for (int i = 0; i < noofpoints; i++)
    ++map_x[xs[i]];

for (int i = 0; i < noofpoints; i++)
    ++map_x[ys[i]];

for (int i = 0; i < noofpoints; i++)
    count += (map_x[xs[i]] - 1) * (map_y[ys[i]] - 1);
std::map map_x;//对于每个x,计算x=x的点
std::map_y;//对于每个y,计算y=y的点
for(int i=0;i
关于最后一个循环的说明:

它迭代所有点,并计算该点位于直角位置的直角三角形的数量

对于每个点,它知道有多少点具有相同的X和多少点具有相同的Y(必须减去1才能排除相同的点)。这些点的每一个组合都给出一个合适的三角形

顺便说一句,有些三角形可能退化(面积为零);此外,如果有些点出现两次,