結果

問題 No.49 算数の宿題
ユーザー sinosino
提出日時 2020-04-13 12:08:44
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 2,386 bytes
コンパイル時間 617 ms
コンパイル使用メモリ 144,008 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-24 17:46:34
合計ジャッジ時間 1,276 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,380 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#![allow(unused_imports)]
#![allow(non_snake_case)]
use std::collections::HashMap;
use std::collections::HashSet;

#[allow(unused_macros)]
macro_rules! read {
    ([$t:ty] ; $n:expr) =>
        ((0..$n).map(|_| read!([$t])).collect::<Vec<_>>());
    ($($t:ty),+ ; $n:expr) =>
        ((0..$n).map(|_| read!($($t),+)).collect::<Vec<_>>());
    ([$t:ty]) =>
        (rl().split_whitespace().map(|w| w.parse().unwrap()).collect::<Vec<$t>>());
    ($t:ty) =>
        (rl().parse::<$t>().unwrap());
    ($($t:ty),*) => {{
        let buf = rl();
        let mut w = buf.split_whitespace();
        ($(w.next().unwrap().parse::<$t>().unwrap()),*)
    }};
}

#[allow(dead_code)]
fn rl() -> String {
    let mut buf = String::new();
    std::io::stdin().read_line(&mut buf).unwrap();
    buf.trim_end().to_owned()
}

enum Token {
    Num(u32),
    Op(Op),
}

enum Op {
    Plus, Mult
}

fn calc(t: &[Token]) -> u32 {
    let mut acc = 0;
    if let Token::Num(n) = t[0] {
        acc = n;
    }

    let mut index = 1;

    while index < t.len()-1 {
        if let Token::Op(o) = &t[index] {
            if let Token::Num(n) = &t[index+1] {
                match o {
                    Op::Plus => acc += n,
                    Op::Mult => acc *= n,
                }
                index += 2;
                continue;
            }
        }
    }

    acc
}

fn tokanizer(s: &[char]) -> Vec<Token> {
    let mut index = 0;
    let len = s.len();
    let mut v = Vec::<Token>::new();

    while index < len {
        match s[index] {
            '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9' => {
                let (t, c) = perse_num(&s[index..]);
                v.push(t);
                index += c;
            },
            '*' => { v.push(Token::Op(Op::Plus)); index += 1; }
            '+' => { v.push(Token::Op(Op::Mult)); index += 1; }
            _   => { unreachable!() }
        }    
    }
    v
}

fn perse_num(s: &[char]) -> (Token, usize) {
    let mut acc = 0;
    let mut count = 0;
    for c in s {
        if let Some(d) = c.to_digit(10) {
            acc *= 10;
            acc += d;
            count += 1;
        }
        else {
            break;
        }
    }
    (Token::Num(acc), count)
}

fn main() {
    let s: Vec<char> = read!(String).chars().fold(vec![], |mut acc, c| {
        acc.push(c);
        acc
    });

    println!("{}", calc(&tokanizer(&s)));
}
0