파게로그

[백준 3009번] 네 번째 점 본문

콤퓨타 왕왕기초/PS

[백준 3009번] 네 번째 점

파게 2020. 10. 30. 14:15

문제 링크: 3009번 네 번째 점

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

 

원리는 쉬운데 구현에서 은근히 고민을 엄청 많이 한 문제이다. 실력의 부족을 여실히 드러내주는 문제...

파이썬 덕을 많이 본 문제라서, C++로도 풀어봐야겠다.

 

개수를 세어서 어딘가 저장하는 것에 대해서 무언가 방법이 있을 것 같은데...

 

Python

xs = []
ys = []
x = -1
y = -1

for i in range(3):
    a, b = map(int, input().split())
    xs.append(a)
    ys.append(b)

for item in xs:
    if xs.count(item) == 1:
        x = item
for item in ys:
    if ys.count(item) == 1:
        y = item

print(x, y)

 

C++

#include <iostream>
using namespace std;

int countElem(int *arr, int len, int item) {
    int res = 0;
    for (int i = 0; i < len; i++)
        if (arr[i] == item)
            res++;
    return res;
}

int main(void) {
    int answerX;
    int answerY;
    int xs[3];
    int ys[3];
    
    for (int i = 0; i < 3; i++) {
        int a, b;
        cin >> a >> b;
        xs[i] = a;
        ys[i] = b;
    }
    
    for (int i = 0; i < 3; i++) {
        if (countElem(xs, 3, xs[i])==1)
            answerX = xs[i];
        if (countElem(ys, 3, ys[i])==1)
            answerY = ys[i];
    }
    
    cout << answerX << ' ' << answerY;
    
    return 0;
}

 

C++ 다른 풀이(XOR 비트연산 이용)

#include <iostream>

using namespace std;

int main(void) {
	int x[3];
    int y[3];
	int answerX = 0;
    int answerY = 0;
    
	for (int i = 0; i < 3; i++) {
		cin >> x[i] >> y[i];
		answerX ^= x[i];
		answerY ^= y[i];
	}
    
	cout << answerX << ' ' << answerY;
	
    return 0;
}
Comments