結果

問題 No.81 すべて足すだけの簡単なお仕事です。
ユーザー tabataba
提出日時 2024-07-31 15:30:13
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 2,840 bytes
コンパイル時間 13,458 ms
コンパイル使用メモリ 398,172 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-07-31 15:30:28
合計ジャッジ時間 14,753 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
6,812 KB
testcase_01 AC 1 ms
6,816 KB
testcase_02 AC 0 ms
6,816 KB
testcase_03 AC 1 ms
6,940 KB
testcase_04 AC 1 ms
6,944 KB
testcase_05 AC 1 ms
6,944 KB
testcase_06 AC 1 ms
6,940 KB
testcase_07 AC 1 ms
6,940 KB
testcase_08 AC 1 ms
6,940 KB
testcase_09 AC 1 ms
6,940 KB
testcase_10 AC 1 ms
6,940 KB
testcase_11 AC 1 ms
6,940 KB
testcase_12 AC 1 ms
6,940 KB
testcase_13 AC 1 ms
6,944 KB
testcase_14 AC 0 ms
6,944 KB
testcase_15 AC 1 ms
6,940 KB
testcase_16 AC 1 ms
6,940 KB
testcase_17 AC 1 ms
6,944 KB
testcase_18 AC 1 ms
6,944 KB
testcase_19 AC 1 ms
6,940 KB
testcase_20 AC 1 ms
6,940 KB
testcase_21 AC 1 ms
6,944 KB
testcase_22 AC 1 ms
6,940 KB
testcase_23 AC 1 ms
6,940 KB
testcase_24 AC 1 ms
6,944 KB
testcase_25 AC 1 ms
6,940 KB
testcase_26 AC 1 ms
6,944 KB
testcase_27 AC 1 ms
6,940 KB
testcase_28 AC 1 ms
6,940 KB
testcase_29 AC 1 ms
6,944 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: unused imports: `BTreeSet`, `HashMap`, `HashSet`, `VecDeque`, `fmt::Debug`, `io::stdin`, `str::FromStr`, `sync::OnceLock`
 --> src/main.rs:2:19
  |
2 |     collections::{BTreeSet, HashMap, HashSet, VecDeque},
  |                   ^^^^^^^^  ^^^^^^^  ^^^^^^^  ^^^^^^^^
3 |     fmt::Debug,
  |     ^^^^^^^^^^
4 |     io::stdin,
  |     ^^^^^^^^^
5 |     str::FromStr,
  |     ^^^^^^^^^^^^
6 |     sync::OnceLock,
  |     ^^^^^^^^^^^^^^
  |
  = note: `#[warn(unused_imports)]` on by default

warning: unused import: `proconio::marker::Chars`
 --> src/main.rs:9:5
  |
9 | use proconio::marker::Chars;
  |     ^^^^^^^^^^^^^^^^^^^^^^^

warning: unused variable: `i`
  --> src/main.rs:92:20
   |
92 |             .map(|(i, v)| format!("{v}"))
   |                    ^ help: if this is intentional, prefix it with an underscore: `_i`
   |
   = note: `#[warn(unused_variables)]` on by default

ソースコード

diff #

use std::{
    collections::{BTreeSet, HashMap, HashSet, VecDeque},
    fmt::Debug,
    io::stdin,
    str::FromStr,
    sync::OnceLock,
};

use proconio::marker::Chars;

fn main() {
    proconio::input! {
        n: usize,
        a: [String; n],
    }
    let a = a
        .into_iter()
        .map(|a| {
            let sign = a.starts_with('-');
            let mut value = [0i8; 30];
            let s = a.split('.').collect::<Vec<_>>();
            let mut v = s[0].parse::<i64>().unwrap().abs();
            let mut idx = 19;
            while v != 0 {
                value[idx] = (v % 10) as i8;
                idx -= 1;
                v /= 10;
            }
            if s.len() == 2 {
                for (i, v) in s[1].chars().map(|c| c.to_digit(10).unwrap()).enumerate() {
                    value[20 + i] = v as i8;
                }
            }
            (sign, value)
        })
        .collect::<Vec<_>>();

    let mut sign = false;
    let mut value = [0i8; 30];
    for (a_sign, a_value) in a {
        match (sign, a_sign) {
            (true, true) | (false, false) => {
                value.iter_mut().zip(a_value.iter()).for_each(|(v, av)| {
                    *v += *av;
                });
            }
            (true, false) => {
                sign = value > a_value;
                if sign {
                    value.iter_mut().zip(a_value.iter()).for_each(|(v, av)| {
                        *v = *v - *av;
                    });
                } else {
                    value.iter_mut().zip(a_value.iter()).for_each(|(v, av)| {
                        *v = *av - *v;
                    });
                }
            }
            (false, true) => {
                sign = value < a_value;
                if sign {
                    value.iter_mut().zip(a_value.iter()).for_each(|(v, av)| {
                        *v = *av - *v;
                    });
                } else {
                    value.iter_mut().zip(a_value.iter()).for_each(|(v, av)| {
                        *v = *v - *av;
                    });
                }
            }
        }
        //normalize
        for i in (1..30).rev() {
            while value[i] > 9 {
                value[i - 1] += 1;
                value[i] -= 10;
            }
            while value[i] < 0 {
                value[i - 1] -= 1;
                value[i] += 10;
            }
        }
    }
    println!(
        "{}{}.{}",
        if sign { "-" } else { "" },
        value
            .iter()
            .take(20)
            .enumerate()
            .skip_while(|(i, v)| *i != 19 && **v == 0)
            .map(|(i, v)| format!("{v}"))
            .collect::<String>(),
        value
            .iter()
            .skip(20)
            .map(|i| format!("{i}"))
            .collect::<String>(),
    );
}
0