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(" ")
相关推荐
先吃饱再说4 小时前
判断回文字符串,从一行代码到双指针优化
算法
黄敬峰7 小时前
深入理解算法核心:从递归思想、数组扁平化到快速排序
算法
得物技术8 小时前
从狂野代码到按目标生产:得物推荐 AI Harness 的工程化实践|AICon 演讲整理
人工智能·算法·架构
AI小老六12 小时前
SkillOpt 架构拆解:把 Skill 文本当参数,用执行轨迹训练 Agent
后端·算法·ai编程
胡萝卜术12 小时前
从“分数打架”到“排名投票”:为什么你的ChatBI必须用RRF?
算法·设计模式·面试
doiito13 小时前
【Agent Harness】Gliding Horse 设计细节 -- 不跟风开发自己的AI Agent
架构·rust·agent
Asize13 小时前
初识DFS 与 BFS:递归、队列与图遍历
算法
doiito15 小时前
【Agent Harness】Gliding Horse 核心设计理念,不跟风开发自己的AI Agent
ai·rust·架构设计·系统设计·ai agent
花褪残红青杏小1 天前
Rust图像处理第6节- 均值模糊 & 中值模糊:3×3 邻域的两种经典玩法
rust·webassembly·图形学