Rust > 牛客OJ在线编程常见输入输出练习场

Rust处理输入输出

牛客OJ在线编程常见输入输出练习场

https://ac.nowcoder.com/acm/contest/5647

A A+B(1)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let nums: Vec<i32> = line.split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
        println!("{}", nums[0] + nums[1]);
    }
}

B A+B(2)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let t: i32 = lines.next().unwrap().unwrap().trim().parse().unwrap();

    for _ in 0..t {
        let line = lines.next().unwrap().unwrap();
        let nums: Vec<i32> = line.split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
        println!("{}", nums[0] + nums[1]);
    }
}

C A+B(3)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let nums: Vec<i32> = line.split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
        if nums[0] == 0 && nums[1] == 0 {
            break;
        }
        println!("{}", nums[0] + nums[1]);
    }
}

D A+B(4)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let nums: Vec<i32> = line.split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
        if nums[0] == 0 {
            break;
        }
        let sum: i32 = nums[1..].iter().sum();
        println!("{}", sum);
    }
}

E A+B(5)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let t: i32 = lines.next().unwrap().unwrap().trim().parse().unwrap();

    for _ in 0..t {
        let line = lines.next().unwrap().unwrap();
        let nums: Vec<i32> = line.split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
        let sum: i32 = nums[1..].iter().sum();
        println!("{}", sum);
    }
}

F A+B(6)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let nums: Vec<i32> = line.split_whitespace()
        .map(|x| x.parse().unwrap())
        .collect();
        let sum: i32 = nums[1..].iter().sum();
        println!("{}", sum);
    }
}

G A+B(7)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let sum: i32 = line.split_whitespace()
        .map(|x| x.parse::<i32>().unwrap())
        .sum();
        println!("{}", sum);
    }
}

H 字符串排序(1)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    let mut lines = stdin.lock().lines();
    let _n: i32 = lines.next().unwrap().unwrap().trim().parse().unwrap();

    let line = lines.next().unwrap().unwrap();
    let mut strs: Vec<&str> = line.split_whitespace().collect();
    strs.sort();
    println!("{}", strs.join(" "));
}

I 字符串排序(2)

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let mut strs: Vec<&str> = line.split_whitespace().collect();
        strs.sort();
        println!("{}", strs.join(" "));
    }
}

J 字符串排序(3)

链接:https://ac.nowcoder.com/acm/contest/5647/J

输入描述:

多个测试用例,每个测试用例一行。

每行通过,隔开,有n个字符,n<100

输出描述:

对于每组用例输出一行排序后的字符串,用','隔开,无结尾空格

rust 复制代码
use std::io::{self, BufRead};

fn main() {
    let stdin = io::stdin();
    for line in stdin.lock().lines() {
        let line = line.unwrap();
        let mut strs: Vec<&str> = line.trim().split(',').collect();
        strs.sort();
        println!("{}", strs.join(","));
    }
}

Rust输入输出要点总结

场景 写法
引入IO模块 use std::io::{self, BufRead};
获取stdin锁 let stdin = io::stdin();
遍历所有行 for line in stdin.lock().lines()
解包行内容 let line = line.unwrap();
按空格分割 line.split_whitespace()
转换为整数Vec .map(|x| x.parse().unwrap()).collect()
求和 nums.iter().sum()
字符串排序 strs.sort()
用分隔符连接 strs.join(" ")
相关推荐
宵时待雨6 分钟前
C++笔记归纳13:map & set
开发语言·数据结构·c++·笔记·算法
1104.北光c°1 小时前
滑动窗口HotKey探测机制:让你的缓存TTL更智能
java·开发语言·笔记·程序人生·算法·滑动窗口·hotkey
仰泳的熊猫5 小时前
题目2570:蓝桥杯2020年第十一届省赛真题-成绩分析
数据结构·c++·算法·蓝桥杯
无极低码8 小时前
ecGlypher新手安装分步指南(标准化流程)
人工智能·算法·自然语言处理·大模型·rag
软件算法开发9 小时前
基于海象优化算法的LSTM网络模型(WOA-LSTM)的一维时间序列预测matlab仿真
算法·matlab·lstm·一维时间序列预测·woa-lstm·海象优化
superior tigre9 小时前
22 括号生成
算法·深度优先
努力也学不会java11 小时前
【缓存算法】一篇文章带你彻底搞懂面试高频题LRU/LFU
java·数据结构·人工智能·算法·缓存·面试
旖-旎11 小时前
二分查找(x的平方根)(4)
c++·算法·二分查找·力扣·双指针
ECT-OS-JiuHuaShan11 小时前
朱梁万有递归元定理,重构《易经》
算法·重构