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(" ")
相关推荐
小O的算法实验室1 天前
2026年ASOC,基于深度强化学习的无人机三维复杂环境分层自适应导航规划方法,深度解析+性能实测
算法·无人机·论文复现·智能算法·智能算法改进
Tomhex1 天前
Rust数组与Vec的核心差异解析
rust
郭涤生1 天前
STL vector 扩容机制与自定义内存分配器设计分析
c++·算法
༾冬瓜大侠༿1 天前
vector
c语言·开发语言·数据结构·c++·算法
Ricky111zzz1 天前
leetcode学python记录1
python·算法·leetcode·职场和发展
汀、人工智能1 天前
[特殊字符] 第58课:两个正序数组的中位数
数据结构·算法·数据库架构··数据流·两个正序数组的中位数
liu****1 天前
第16届省赛蓝桥杯大赛C/C++大学B组(京津冀)
开发语言·数据结构·c++·算法·蓝桥杯
汀、人工智能1 天前
[特殊字符] 第79课:分割等和子集
数据结构·算法·数据库架构·位运算·哈希表·分割等和子集
汀、人工智能1 天前
[特殊字符] 第74课:完全平方数
数据结构·算法·数据库架构·图论·bfs·完全平方数
CoderCodingNo1 天前
【GESP】C++四、五级练习题 luogu-P1177 【模板】排序
数据结构·c++·算法