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(" ")
相关推荐
To_OC7 小时前
LC 131 分割回文串:刚学回溯时,我连怎么切字符串都想不明白
javascript·算法·leetcode
旖-旎8 小时前
LeetCode 518:零钱兑换||(完全背包)—— 题解
c++·算法·leetcode·动态规划·背包问题
To_OC8 小时前
LC 42 接雨水:暴力超时卡半天?前后缀数组一用就通了
javascript·算法·leetcode
delishcomcn9 小时前
AI视觉识别+分切算法:电化铝缺陷检测与裁切一体化解锁
人工智能·算法
触底反弹9 小时前
深入理解大模型采样:Temperature、Top-K、Top-P 的原理与实战
人工智能·算法·面试
雪碧聊技术10 小时前
力扣 LCR 091. 粉刷房子 —— 动态规划入门详解
算法·动态规划
段一凡-华北理工大学11 小时前
向量数据库实战:选型、调优与落地~系列文章12:文本分块策略实战:chunk_size 怎么选?重叠多少?
开发语言·数据库·后端·oracle·rust·工业智能体·高炉智能化
CV-Climber12 小时前
检索技术的实际应用
人工智能·算法
hhzz13 小时前
Tiger AI Platform平台中增加人脸识别功能
图像处理·人工智能·算法·计算机视觉·大模型