https://www.acmicpc.net/problem/3009

 

세점을 가지고 축에 평행한 직사각형을 만들기 위한 네번째 점을 찾아야한다. 2번나온 x,y좌표를 제외하고 한번만 나온 x,y값을 사용하여 좌표를 완성해주자

 

나는 모든값을 받고  2번이상나왔는지 if문을 통해 확인했지만 더 효율적으로 푸는 방법은 모든 값을 받고 XOR 연산을 통해 X,Y값을 얻어내는 것이 가장 효율적이다 .

 

정답코드1

#include <iostream>
#include <vector>
#include <utility>

using namespace std;

int main() {
    vector<pair<int, int>> vp(3); // 세 점을 받으므로 크기 3으로 설정

    for (int i = 0; i < 3; i++) {
        cin >> vp[i].first >> vp[i].second; // 세 점의 좌표 입력
    }

    int x, y;

    // X 좌표 구하기
    if (vp[0].first == vp[1].first) {
        x = vp[2].first;
    }
    else if (vp[0].first == vp[2].first) {
        x = vp[1].first;
    }
    else {
        x = vp[0].first;
    }

    // Y 좌표 구하기
    if (vp[0].second == vp[1].second) {
        y = vp[2].second;
    }
    else if (vp[0].second == vp[2].second) {
        y = vp[1].second;
    }
    else {
        y = vp[0].second;
    }

    cout << x << " " << y << endl; // 네 번째 점 출력

    return 0;
}

정답코드2(XOR연산사용)

#include <iostream>

using namespace std;

int main() {
    int x1, y1, x2, y2, x3, y3;
    cin >> x1 >> y1 >> x2 >> y2 >> x3 >> y3;

    // XOR 연산을 통해 x 좌표와 y 좌표를 구한다
    int x4 = x1 ^ x2 ^ x3;
    int y4 = y1 ^ y2 ^ y3;

    cout << x4 << " " << y4 << endl;

    return 0;
}

+ Recent posts