题解 | #合并表记录#
合并表记录
https://www.nowcoder.com/practice/de044e89123f4a7482bd2b214a685201
use std::io::{self, *}; use std::collections::HashMap; fn main() { let mut input = String::new(); std::io::stdin().read_line(&mut input).expect("input failed."); // println!("input {}", input); let num = input.trim().parse().expect("can not parse this num"); let mut map = HashMap::new(); for _ in 0..num { let mut input = String::new(); std::io::stdin().read_line(&mut input).expect("input failed."); let arr: Vec<i32> = input.split_whitespace() .map(|s| s.parse().expect("input failed2")) .collect(); match map.get(&arr[0]) { Some(num) => { map.insert(arr[0], num+arr[1]); }, None => { map.insert(arr[0], arr[1]); }, } } let mut keys: Vec<_> = map.keys().collect(); keys.sort(); // println!("result: "); for key in keys { if let Some(v) = map.get(key) { println!("{} {}", key, v); } } }