Rust Sample - Input Parsing

mohadang·2023년 7월 30일
0

Rust Sample

목록 보기
1/16
post-thumbnail

read_line

use std::io;

fn main() {
    let mut inputs = String::new();
    if let Ok(_) = io::stdin().read_line(&mut inputs) {
        inputs.pop();
        println!("{inputs}??!");
    }
}
use std::io;

fn main() {
    let mut inputs = String::new();
    if let Ok(_) = io::stdin().read_line(&mut inputs) {
        let mut sp = inputs.split_whitespace();
        if let (Some(a), Some(b), Some(c)) = (sp.next(), sp.next(), sp.next()) {
            if let (Ok(a), Ok(b), Ok(c)) = (a.parse::<i32>(), b.parse::<i32>(), c.parse::<i32>()) {
                println!("{}", (a + b) % c);
                println!("{}", ((a % c) + (b % c)) % c);
                println!("{}", (a * b) % c);
                println!("{}", ((a % c) * (b % c)) % c);
            }
        }
    }
}
use std::io;

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        let mut sp = input.split_ascii_whitespace();
        if let (Some(n), Some(m)) = (sp.next(), sp.next()) {
            if let (Ok(n), Ok(m)) = (n.parse::<usize>(), m.parse::<u32>()) {
                let mut arr: Vec<u32> = vec![0; n];
                for _ in 0..m {
                    input.clear();
                    if let Ok(_) = io::stdin().read_line(&mut input) {
                        let mut sp = input.split_ascii_whitespace();
                        if let (Some(i), Some(j), Some(k)) = (sp.next(), sp.next(), sp.next()) {
                            if let (Ok(i), Ok(j), Ok(k)) = (i.parse::<usize>(), j.parse::<usize>(), k.parse::<u32>()) {
                                if i > n {
                                    continue;
                                }
                                for x in i.min(n)..=j.min(n) {
                                    arr[x - 1] = k;
                                }
                            }
                        }
                    }
                }

                println!("{}", arr.iter().map(ToString::to_string).collect::<Vec<String>>().join(" "));
            }
        }
    }
}
use std::io::{self, Read};

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_line(&mut input) {
        let mut sp = input.split_ascii_whitespace();
        if let (Some(n), Some(m)) = (sp.next(), sp.next()) {
            if let (Ok(n), Ok(m)) = (n.parse::<usize>(), m.parse::<usize>()) {
                let mut v = (0..n).map(|x| (x + 1).to_string()).collect::<Vec<String>>();
                (0..m).for_each(|_| {
                    input.clear();
                    if let Ok(_) = io::stdin().read_line(&mut input) {
                        let mut sp = input.split_ascii_whitespace();
                        if let (Some(i), Some(j)) = (sp.next(), sp.next()) {
                            if let (Ok(i), Ok(j)) = (i.parse::<usize>(), j.parse::<usize>()) {
                                v[(i - 1)..=(j - 1)].reverse();
                            }
                        }
                    }
                });
                println!("{}", v.join(" "));
            }
        }
    }
}

read_to_end

use std::io::Read;
fn main() -> std::io::Result<()> {
    let mut buffer: Vec<u8> = Vec::new();
    std::io::stdin().read_to_end(&mut buffer)?;
    if let Some(ch) = buffer.first() {
        println!("{ch}");
    }
    Ok(())
}

read_to_string

use std::io::{self, Write, Read};

fn main() {
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_to_string(&mut input) {
        input.split('\n').for_each(|x| {
            let s = x
                .split_ascii_whitespace()
                .filter_map(|y| y.parse::<u32>().ok())
                .sum::<u32>();
            if s != 0 {
                writeln!(out, "{s}", ).unwrap()
            }
        });
    }
}

read_exact

use std::io::{self, Read};

fn main() -> io::Result<()> {
    let mut input = String::new();
    io::stdin().read_line(&mut input)?;
    if let Ok(n) = input.trim().parse::<usize>() {
        let mut buffer: Vec<u8> = vec![0; n];
        io::stdin().read_exact(&mut buffer)?;
        println!("{}", 
            buffer
            .iter()
            .filter_map(|&x| char::from(x).to_digit(10))
            .sum::<u32>());
    }
    Ok(())
}

lines

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

fn main() {
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines().map(|line| line.unwrap());

    if let Ok(n) = lines.next().unwrap().trim().parse::<usize>() {
    	// task(n) : n개 라인들 획득
        lines.take(n).enumerate().for_each(|(i, line)| { 
            let numbers: Vec<i32> = line
                .split_whitespace()
                .map(|num| num.parse().unwrap())
                .collect();
            let sum: i32 = numbers.iter().sum();
            writeln!(out, "Case #{}: {} + {} = {}", i + 1, numbers[0], numbers[1], sum).unwrap();
        });
    }

    out.flush().unwrap();
}
profile
mohadang

0개의 댓글