题解 | #A+B(1)#

A+B(1)

https://ac.nowcoder.com/acm/contest/5652/A

RUST字符串排序

#![allow(dead_code)]

use std::io;
use std::fmt;

#[derive(Debug)]
pub struct Inputs {
    pub v: Vec<String>,
}

impl Inputs {
    fn from_vec(ve: Vec<&str>) -> Inputs {
        let mut v = Vec::new();
        for i in 0..ve.len() {
            v.push(ve[i].to_owned());
        }
        Inputs { v }
    }
    fn solve(&mut self) -> () {
        self.v.sort();
        println!("{}", self);
    }
}

impl fmt::Display for Inputs {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut i = 0usize;
        for e in self.v.iter() {
            write!(f, "{}", e)?;
            i += 1;
            if i < self.v.len() {
                write!(f, ",")?;
            }
        }
        Ok(())
    }
}

pub fn main() {
    loop {
        match input() {
            Some(mut input) => input.solve(),
            None => break
        }
    }
}

pub fn input() -> Option<Inputs> {
    let mut input = String::new();
    match io::stdin().read_line(&mut input) {
        Ok(_n) => {
            let s = input.trim_end();
            match s.len() {
                0 => None,
                _ => {
                    let arr: Vec<&str> = s.split(',').collect();
                    Some(Inputs::from_vec(arr))
                }
            }
        }
        Err(error) => panic!("error: {}", error),
    }
}
全部评论

相关推荐

找不到工作死了算了:没事的,雨英,hr肯主动告知结果已经超越大部分hr了
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务