Update solution for 스택 w/ id 19446155

Time: 4ms
MemUsage: 13148KB
This commit is contained in:
2024-08-29 16:22:27 +09:00
parent 7938c44dd1
commit e68cda5701

View File

@@ -0,0 +1,60 @@
use std::io::{self, BufRead, Write, BufWriter};
fn main(){
let mut stack : Vec<u32> = Vec::new();
let stdin = io::stdin();
let mut num_command = String::new();
stdin.read_line(&mut num_command).expect("Failed to read line");
let num_command = num_command.trim().parse::<usize>().expect("Not an integer");
let lock_in = stdin.lock();
let stdout = io::stdout();
let lock_out = stdout.lock();
let mut buff = BufWriter::new(lock_out);
for (idx, line) in lock_in.lines().enumerate(){
if idx == num_command{
break;
}
let inputs = line.unwrap();
let mut inputs = inputs.trim().split(" ");
let command = inputs.next().expect("Not enough input");
match command {
"push" => {
let n = inputs.next().expect("Not enough input")
.parse::<u32>().expect("Not an integer");
stack.push(n);
},
"pop" => {
match stack.pop(){
Some(n) => buff.write_fmt(format_args!("{}\n", n)),
None => buff.write_fmt(format_args!("{}\n", -1)),
};
},
"size" => {
buff.write_fmt(format_args!("{}\n", stack.len()));
},
"top" => {
match stack.pop(){
Some(n) => {
buff.write_fmt(format_args!("{}\n", n));
stack.push(n);
},
None => {
buff.write_fmt(format_args!("{}\n", -1));
},
};
}
"empty" => {
match stack.is_empty(){
true => buff.write_fmt(format_args!("{}\n", 1)),
false => buff.write_fmt(format_args!("{}\n", 0)),
};
}
_ => continue,
}
}
}