본문 바로가기

컴퓨터 과학/자료구조, 알고리즘

[Rust로 백준 하루 하나] 10-3. 네 번째 점

문제 (3009번)

세 점이 주어졌을 때, 축에 평행한 직사각형을 만들기 위해서 필요한 네 번째 점을 찾는 프로그램을 작성하시오.

 

입력

세 점의 좌표가 한 줄에 하나씩 주어진다. 좌표는 1보다 크거나 같고, 1000보다 작거나 같은 정수이다.

 

출력

직사각형의 네 번째 점의 좌표를 출력한다.


풀이

코드

use std::io::{self, BufReader, BufRead, BufWriter, Write};

fn main() {
    let mut reader = BufReader::new(io::stdin().lock());
    let mut writer = BufWriter::new(io::stdout().lock());

    let mut xs: Vec<i32> = Vec::new();
    let mut ys: Vec<i32> = Vec::new();

    let mut input = String::new();
    for _ in 0..3 {
        reader.read_line(&mut input).unwrap();
        let mut input_iter = input
            .trim()
            .split_whitespace()
            .map(|i| i.parse().unwrap());
        let x = input_iter.next().unwrap();
        let y = input_iter.next().unwrap();
        xs.push(x);
        ys.push(y);
        input.clear();
    }

    let fourth_x = find_unique(&xs);
    let fourth_y = find_unique(&ys);
    writeln!(writer, "{} {}", fourth_x, fourth_y).unwrap();
}

fn find_unique(input: &Vec<i32>) -> i32 {
    if input[0] == input[1] {
        input[2]
    } else if input[0] == input[2] {
        input[1]
    } else {
        input[0]
    }
}

해설

특이사항 없음


추가 학습

  • 특이사항 없음