Rust Sample - iterator

mohadang·2023년 7월 30일
0

Rust Sample

목록 보기
7/16
post-thumbnail

iter

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

fn main() {
    let mut input = String::new();
    if let Ok(_) = io::stdin().read_to_string(&mut input) {
        let mut arr = [false; 30];
        let vec = input
            .trim()
            .split('\n')
            .filter_map(|x| x.parse::<usize>().ok())
            .collect::<Vec<usize>>();
        for &num in vec.iter() {
            arr[num - 1] = true;
        }
        for (idx, &flag) in arr.iter().enumerate() {
            if !flag {
                println!("{}", idx + 1);
            }
        }
    }
}

map

use std::io;

fn main() {
    let mut inputs = String::new();
    if let Ok(_) = io::stdin().read_line(&mut inputs) {
        println!(
            "{}",
            inputs
                .split_whitespace()
                .map(|x| x.parse::<u64>().unwrap())
                .sum::<u64>()
        );
    }
}

filter_map

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

fn main() {
    let stdout = io::stdout();
    let mut out = io::BufWriter::new(stdout.lock());
    let mut input = String::new();
    loop {
        input.clear();
        if let Ok(_) = io::stdin().read_line(&mut input) {
            match input
                .split_ascii_whitespace()
                .filter_map(|x| x.parse::<u32>().ok())
                .sum::<u32>()
            {
                0 => {
                    break;
                }
                x => {
                    writeln!(out, "{x}").unwrap();
                }
            }
        }
    }
}
fn main() {
    let mut input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut input) {
        if let Ok(n) = input.trim().parse::<usize>() {
            (0..n).for_each(|_| {
                input.clear();
                if let Ok(_) = std::io::stdin().read_line(&mut input) {
                    let sum = input
                        .split_whitespace()
                        .filter_map(|x| x.parse::<i32>().ok())
                        // .map(|num| num.parse::<i32>().unwrap_or(0))
                        // .map(|x| x.parse::<i32>().unwrap())//not good
                        .sum::<i32>();
                    println!("{}", sum);
                }
            });
        }
    }
}

filter

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

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_line(&mut input) {
        let mut sp = input.split_ascii_whitespace();
        if let (Some(n), Some(x)) = (sp.next(), sp.next()) {
            if let (Ok(_), Ok(x)) = (n.parse::<u32>(), x.parse::<u32>()) {
                input.clear();
                if let Ok(_) = io::stdin().read_line(&mut input) {
                    write!(
                        out,
                        "{}",
                        input
                            .split_ascii_whitespace()
                            .filter_map(|y| y.parse::<u32>().ok())
                            .filter(|&y| y < x)
                            .map(|y| y.to_string())
                            .collect::<Vec<_>>()
                            .join(" ")
                    )
                    .unwrap();
                }
            }
        }
    }
}

sum

use std::io::Write;

fn main() {
    let stdout = std::io::stdout();
    let mut out = 
        std::io::BufWriter::new(stdout.lock());
    let mut input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut input) {
        if let Ok(n) = input.trim().parse::<usize>() {
            (0..n).for_each(|i| {
                input.clear();
                if let Ok(_) = std::io::stdin().read_line(&mut input) {
                    let sum = input
                        .split_whitespace()
                        .filter_map(|x| x.parse::<i32>().ok())
                        .sum::<i32>();
                    writeln!(out, "Case #{}: {sum}", i + 1).unwrap();
                }
            });
        }
    }
}

fold

use std::io::Write;

fn main() {
    let stdout = std::io::stdout();
    let mut out = 
        std::io::BufWriter::new(stdout.lock());
        
    let mut input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut input) {
        if let Ok(n) = input.trim().parse::<usize>() {
            (0..n).for_each(|_| {
                input.clear();
                if let Ok(_) = std::io::stdin().read_line(&mut input) {
                    let sum = input
                        .split_whitespace()
                        .filter_map(|x| x.parse::<i32>().ok())
                        .fold(0, |acc, x| acc + x);
                    writeln!(out, "{sum}").unwrap();
                }
            });
        }
    }
}
fn main() {
    let mut input1 = String::new();
    let mut input2 = String::new();
    if let (Ok(_), Ok(_)) = (
        std::io::stdin().read_line(&mut input1),
        std::io::stdin().read_line(&mut input2),
    ) {
        if let (Ok(total), Ok(n)) = (
            input1.trim().parse::<usize>(),
            input2.trim().parse::<usize>(),
        ) {
            let sum = (0..n).fold(0, |acc, _| {
                input1.clear();
                if let Ok(_) = std::io::stdin().read_line(&mut input1) {
                    let mut sp = input1.split_whitespace();
                    if let (Some(a), Some(b)) = (sp.next(), sp.next()) {
                        if let (Ok(a), Ok(b)) = (a.parse::<usize>(), b.parse::<usize>()) {
                            return acc + a * b;
                        }
                    }
                } 
                acc
            });
            println!("{}", if total == sum { "Yes" } else { "No" });
        }
    }
}

collect

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

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_line(&mut input) {
        if let Ok(n) = input.trim().parse::<usize>() {
            (0..n).rev().for_each(|i| {
                let stars: String = (0..n).map(|x| if x < i {' '} else {'*'}).collect();
                writeln!(out, "{}", stars).unwrap();
            });
        }
    }
}

join

fn main() {
    let mut input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut input) {
        if let Ok(n) = input.trim().parse::<usize>() {
            let long_str = (1..=(n / 4))
                .map(|_| "long")
                .collect::<Vec<&str>>()
                .join(" ");
            println!("{} int", long_str);
        }
    }
}

max

fn main() {
    let mut input = String::new();
    if let Ok(_) = std::io::stdin().read_line(&mut input) {
        if let Ok(m) = input.trim().parse::<u32>() {
            input.clear();
            if let Ok(_) = std::io::stdin().read_line(&mut input) {
                let vec = input
                    .trim()
                    .split_ascii_whitespace()
                    .filter_map(|x| x.parse::<u32>().ok())
                    .collect::<Vec<u32>>();
                let& max_val = vec.iter().max().unwrap();
                let max_val = max_val as f32;
                let sum = vec
                    .iter()
                    .map(|&x| x as f32)
                    .map(|x| (x / max_val)  * 100.0).sum::<f32>();
                println!("{}", sum / m as f32);
            }
        }
    }
}
profile
mohadang

0개의 댓글