This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center
                            
                        
                    
                
                                6年前关闭。
            
                    
我的代码:

#include "stdafx.h"
#include <vector>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
typedef pair<int,int> point;
int numTorres, numEdificios;
vector<point> towervec;
vector<point> skyvec;
typedef vector<point>::const_iterator tower_iter;
typedef vector<point>::const_iterator sky_iter;

int noofpaths = 0;

int findslope(const point & i, const point & j, const point & k) {
    int dx, dy, dx1, dy1;
    dx = j.first - i.first;
    dy = j.second - i.second;

    int a = dy / dx;
    dx1 = k.first - i.first;
    dy1 = k.second - i.second;

    int b = dy1 / dx1;

    if(a != b) {


        int length1 = (j.first - i.first) * (j.first - i.first) + (j.second - i.second) * (j.second - i.second);
        int length2 = (k.first - i.first) * (k.first - i.first) + (k.second - i.second) * (k.second - i.second);
        if(length1 < length2) {
            noofpaths++;
        }
    }
    return noofpaths;
}

int main() {
    int T;
    int n, m;
    cout << "enter the test cases" << endl;
    cin >> T;
    while(T--) {
        cout << "towers and skyscrapers" << endl;
        cin >> n >> m;


        for(int i = 0; i < n; i++) {
            point p;
            cin >> p.first >> p.second;
            towervec.push_back(p);
            skyvec.push_back(p);

        }
        for(int i = 0; i < m; i++) {
            point p;
            cin >> p.first >> p.second;
            skyvec.push_back(p);
        }



        for(tower_iter i = towervec.begin(); i != towervec.end(); i++) {
            for(tower_iter j = i + 1; j != towervec.end(); j++) {
                for(sky_iter k = skyvec.begin(); k != skyvec.end(); k++) {

                    int dx, dy;

                    noofpaths = findslope(*i, *j, *k);
                    cout << noofpaths;
                }
            }
        }
    }
    return 0;
}


如何克服零误差除法。如果dx = 0或dy = 0或两者都等于零,则代码中断。

如何解决这个错误..例如拿点
i = -1,-1 j = 1,-1,k-1,-1。试图找到3个点是否共线。

最佳答案

设置if条件来检查变量是否为0,从而将带有除法语句的变量设置为0。

10-05 21:08
查看更多