Rust Sample - if/else

mohadang·2023년 7월 30일
0

Rust Sample

목록 보기
4/16
post-thumbnail
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 ans = if (num % 4 == 0 && num % 100 != 0) || (num % 400 == 0) {
                1
            } else {
                0
            };
            println!("{ans}");
        } else {
            println!("Error: Not a number");
        }
    }
}
use std::io;

fn main() {
    let mut input = String::new();
    let Ok(_) = io::stdin().read_line(&mut input) else {
        return;
    };

    let Ok(num) = input.trim().parse::<u32>() else {
        println!("Error: Not a number");
        return;
    };

    let ans = if (num % 4 == 0 && num % 100 != 0) || (num % 400 == 0) {
        1
    } else {
        0
    };
    println!("{ans}");
}
use std::io;

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        let mut sp = input.split_whitespace();
        if let (Some(h), Some(m)) = (sp.next(), sp.next()) {
            if let (Ok(h), Ok(m)) = (h.parse::<i32>(), m.parse::<i32>()) {
                let (hour, min) = if m < 45 { 
                    if h == 0 { 
                        (23, 60 - (45 - m)) 
                    } else { 
                        (h - 1, 60 - (45 - m)) 
                    }
                } else { 
                    (h, m - 45) 
                };
                println!("{hour} {min}");
            }
        }
    }
}
profile
mohadang

0개의 댓글