Rust Sample - match

mohadang·2023년 7월 30일
0

Rust Sample

목록 보기
3/16
post-thumbnail
fn main() {
    let mut input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut input) {
        let mut sp = input.split_whitespace();
        if let (Some(a), Some(b)) = (sp.next(), sp.next()) {
            if let (Ok(a), Ok(b)) = (a.parse::<i32>(), b.parse::<i32>()) {
                match a.cmp(&b) {
                    std::cmp::Ordering::Less => println!("<"),
                    std::cmp::Ordering::Equal => println!("=="),
                    std::cmp::Ordering::Greater => println!(">"),
                }
            }
        }
    }
}
use std::io;

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        if let Ok(num) = input.trim().parse::<u32>() {
            let grade = match num {
                60..=69 => "D",
                70..=79 => "C",
                80..=89 => "B",
                90..=100 => "A",
                _ => "F",
            };
            println!("{}", grade);
        } else {
            println!("Error: Not a number");
        }
    }
}
use std::io;

fn main() {
    let mut input1 = String::new();
    let mut input2 = String::new();
    if let (Ok(_), Ok(_)) = (
        io::stdin().read_line(&mut input1),
        io::stdin().read_line(&mut input2),
    ) {
        if let (Ok(x), Ok(y)) = (input1.trim().parse::<i32>(), input2.trim().parse::<i32>()) {
            let ans = match (x, y) {
                (x, y) if x > 0 && y > 0 => 1,
                (x, y) if x < 0 && y > 0 => 2,
                (x, y) if x < 0 && y < 0 => 3,
                (x, y) if x > 0 && y < 0 => 4,
                _ => 0,
            };
            println!("{}", ans);
        } else {
            println!("Error: Not a number");
        }
    }
}
use std::io;

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        let mut numbers = input.split_whitespace();
        if let (Some(a), Some(b), Some(c)) = (numbers.next(), numbers.next(), numbers.next()) {
            if let (Ok(a), Ok(b), Ok(c)) = (a.parse::<i32>(), b.parse::<i32>(), c.parse::<i32>()) {
                let ans = match (a == b, b == c, a == c) {
                    (true, true, true) => 10_000 + (a * 1_000),
                    (false, false, false) => a.max(b).max(c) * 100,
                    _ => {
                        let match_val = if a == b {
                            a
                        } else if b == c {
                            b
                        } else {
                            c
                        };
                        1_000 + (match_val * 100)
                    }
                };
                println!("{ans}");
            } else {
                println!("Error: Invalid input");
            }
        }
    }
}
profile
mohadang

0개의 댓글