Rust处理输入输出
- 牛客OJ在线编程常见输入输出练习场
- [A A+B(1)](#A A+B(1))
- [B A+B(2)](#B A+B(2))
- [C A+B(3)](#C A+B(3))
- [D A+B(4)](#D A+B(4))
- [E A+B(5)](#E A+B(5))
- [F A+B(6)](#F A+B(6))
- [G A+B(7)](#G A+B(7))
- [H 字符串排序(1)](#H 字符串排序(1))
- [I 字符串排序(2)](#I 字符串排序(2))
- [J 字符串排序(3)](#J 字符串排序(3))
- 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(" ") |