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(" ")
相关推荐
忆湫淮1 小时前
ENVI 5.6 利用现场标准校准板计算地表反射率具体步骤
大数据·人工智能·算法
Ayanami_Reii1 小时前
基础数据结构应用-一个简单的整数问题
数据结构·算法·树状数组·fenwick tree
Ayanami_Reii1 小时前
进阶数据结构应用-一个简单的整数问题2(Fenwick-Tree 解法)
数据结构·算法·前缀和·差分·树状数组·fenwick tree
老黄编程1 小时前
点云生成深度图的原理及算法步骤和参数详细说明
数学·算法·点云·深度图
老黄编程1 小时前
点云SIFT3D特征点云原理、算法描述及参数详细说明
算法·3d·sift3d
老黄编程1 小时前
3DHarris特征提取算法描述及参数详细说明
数学·算法·几何·3dharris特征提取
fruge1 小时前
技术面试复盘:高频算法题的前端实现思路(防抖、节流、深拷贝等)
前端·算法·面试
希望有朝一日能如愿以偿1 小时前
力扣每日一题:使数组和能被p整除
数据结构·算法·leetcode
Christo31 小时前
AAAI-2013《Spectral Rotation versus K-Means in Spectral Clustering》
人工智能·算法·机器学习·数据挖掘·kmeans