結果

問題 No.83 最大マッチング
ユーザー wraiknywraikny
提出日時 2018-05-25 14:08:40
言語 Rust
(1.77.0)
結果
WA  
実行時間 -
コード長 1,512 bytes
コンパイル時間 666 ms
コンパイル使用メモリ 143,640 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-11 03:25:38
合計ジャッジ時間 1,449 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
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,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 1 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 1 ms
4,384 KB
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io;

enum Num {
    One,
    Four,
    Five,
    Seven,
    Nine,
}

impl Num {
    fn value(&self) -> u32 {
        match self {
            Num::One => 1,
            Num::Four => 4,
            Num::Five => 5,
            Num::Seven => 7,
            Num::Nine => 9,
        }
    }

    fn matches(&self) -> u32 {
        match self {
            Num::One => 2,
            Num::Four => 4,
            Num::Five => 5,
            Num::Seven => 3,
            Num::Nine => 6,
        }
    }
}

fn max_num(n : u32) -> Num {
    match n {
        5 => Num::Five,
        4 => Num::Four,
        3 => Num::Seven,
        2 => Num::One,
        _ => Num::Nine,
    }
}

fn calc_digit(n : u32) -> u32 {
    n / Num::One.matches()
}

fn calc_result(r : &Vec<u32>) -> u64 {
    r.iter().rev().enumerate().fold(0, |s, (i, j)| s + 10i64.pow(i as u32) as u64 * (*j as u64))
}

fn main() {
    let mut input = String::new();

    io::stdin().read_line(&mut input).unwrap();

    let mut n : u32 = input.trim().parse().unwrap();

    let digits = calc_digit(n);

    let result : Vec<u32> = (1..digits+1).map(|_| Num::One.value()).collect();

    n = n - digits * Num::One.matches();

    let result = result.iter().map(|x| {
        if n != 0 {
            let match_num = n + Num::One.matches();
            let num = max_num(match_num);
            n = match_num - num.matches();
            num.value()
        } else {
            *x
        }
    }).collect();

    println!("{}", calc_result(&result));
}
0