結果

問題 No.3157 Nabeatsu
ユーザー akkey
提出日時 2025-05-24 11:39:39
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 12 ms / 2,000 ms
コード長 1,648 bytes
コンパイル時間 11,330 ms
コンパイル使用メモリ 398,544 KB
実行使用メモリ 7,936 KB
最終ジャッジ日時 2025-05-24 11:39:55
合計ジャッジ時間 13,842 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 45
権限があれば一括ダウンロードができます

ソースコード

diff #

use proconio::{input, marker::Chars};
use std::iter;
fn main() {
    input! {
        n: Chars
    }
    let pos3 = n.iter().position(|&x| x == '3');
    let rem = n[..pos3.unwrap_or(n.len())].iter().map(|&c| c as usize - '0' as usize).sum::<usize>() % 3;
    match pos3 {
        Some(pos3) if pos3 == n.len() - 1 => {
            if rem == 1 {
                println!("{}1", n[..pos3].iter().collect::<String>())
            } else {
                println!("{}2", n[..pos3].iter().collect::<String>())
            }
        },
        Some(pos3) => {
            println!("{}2{}{}",
                n[..pos3].iter().collect::<String>(),
                iter::repeat('9').take(n.len() - pos3 - 2).collect::<String>(),
                if rem == 1 { '8' } else { '9' });
        },
        None if rem % 3 != 0 => println!("{}", n.into_iter().collect::<String>()),
        None => {
            let last_not0_pos = n.iter().rposition(|&x| x != '0').unwrap();
            let last_not0_int = n[last_not0_pos] as usize - '0' as usize;
            if last_not0_int != 4 {
                println!("{}{}{}",
                    n[..last_not0_pos].iter().collect::<String>(),
                    n[last_not0_pos] as usize - '0' as usize - 1,
                    iter::repeat('9').take(n.len() - last_not0_pos - 1).collect::<String>());
            } else {
                println!("{}{}{}",
                    n[..last_not0_pos].iter().collect::<String>(),
                    n[last_not0_pos] as usize - '0' as usize - 2,
                    iter::repeat('9').take(n.len() - last_not0_pos - 1).collect::<String>());
            }
        }
    }
}
0