본문 바로가기

Algorithm/Implementation

(Rust) - 백준(BOJ) 5087 : Card Cutting

반응형

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

 

5087번: Card Cutting

Cheryl scores 6 points for A, 3, A, 3, A and 5, Tania scores 3 points for 2, 8 and 10. Cheryl scores 4 points for 9, 3, 3 and 7, Tania also scores 4 points for 2, 4, 6 and 8.

www.acmicpc.net

구현 문제였습니다.

📕 풀이방법

📔 입력 및 초기화

줄 정보를 저장할 mut line, cheryl의 점수 cheryl_point, tania점수 tania_point를 선언 해줍니다. 공백으로 구분된 한 줄의 정보를 line에 저장하고 해당 부분으로 구분된 vector를 numbers라는 변수로 선언해 저장합니다.

📔 풀이과정

numbers의 원소를 하나씩 확인하는 for loop를 수행합니다. 1. parse되기 전 "A"같은 점수를 1로 변환해주어 score변수를 선언해 저장합니다. 2. parse후 홀수면 cheryl, 짝수면 tania점수를 1점씩 더해줍니다.

📔 정답출력

조건에 따라 출력합니다.


📕 Code

use std::io;
fn main() {
    loop {
        let mut line = String::new();
        let mut cheryl_point = 0;
        let mut tania_point = 0;
        io::stdin().read_line(&mut line).expect("wrong io");
        line = line.trim().to_owned();

        if line == "#".to_owned() {
            break;
        }

        let numbers: Vec<&str> = line.split_whitespace().collect();
        for i in numbers {
            if i == "*" {
                break;
            }
            let score;

            match i {
                "A" => score = "1",
                _ => score = i,
            }

            let score: i32 = score.parse().unwrap();

            if score % 2 > 0 {
                cheryl_point += 1;
            } else {
                tania_point += 1;
            }
        }

        if cheryl_point > tania_point {
            println!("Cheryl");
        } else if cheryl_point < tania_point {
            println!("Tania");
        } else {
            println!("Draw");
        }
    }
}

*더 나은 내용을 위한 지적, 조언은 언제나 환영합니다.