結果

問題 No.222 引き算と足し算
ユーザー tonyu0
提出日時 2020-04-26 19:06:40
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 1 ms / 1,000 ms
コード長 1,045 bytes
コンパイル時間 13,775 ms
コンパイル使用メモリ 383,696 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-17 18:31:10
合計ジャッジ時間 15,185 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::*;

fn parse(s: &[char]) -> i64 {
    let mut i = 0;
    let mut mul = 1;
    if s[0] == '+' || s[0] == '-' {
        i += 1;
        if s[0] == '-' {
            mul = -1;
        }
    }

    let mut res = 0;
    while i < s.len() {
        res = res * 10 + (s[i] as u8 - '0' as u8) as i64;
        i += 1;
    }
    res * mul
}

fn main() {
    let mut s: String = String::new();
    std::io::stdin().read_to_string(&mut s).ok();
    let mut itr = s.trim().split_whitespace();
    let s: Vec<char> = itr.next().unwrap().chars().collect();

    let mut num = Vec::new();
    let mut tmp = Vec::new();
    let mut ops = ' ';
    for i in 0..s.len() {
        if 0 < i && ops == ' ' && (s[i] == '+' || s[i] == '-') {
            ops = s[i];
            num.push(parse(&tmp));
            tmp.clear();
        } else {
            tmp.push(s[i]);
        }
    }
    num.push(parse(&tmp));
    println!(
        "{}",
        if ops == '+' {
            num[0] - num[1]
        } else {
            num[0] + num[1]
        }
    )
}
0