結果

問題 No.39 桁の数字を入れ替え
ユーザー sinosino
提出日時 2020-03-26 09:41:57
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 2,074 bytes
コンパイル時間 1,652 ms
コンパイル使用メモリ 149,020 KB
実行使用メモリ 4,504 KB
最終ジャッジ日時 2023-08-30 12:10:12
合計ジャッジ時間 2,524 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

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

#[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()
}

trait IteratorExt: Iterator + Sized {
    fn vec(self) -> Vec<Self::Item> {
        self.collect()
    }
}
impl<T: Iterator> IteratorExt for T {}

fn main() {
    let n = read!(String);
    let mut v = {
        let mut tmp = vec![];
        for d in n.chars() {
            let d = match d {
                '1' => 1,
                '2' => 2,
                '3' => 3,
                '4' => 4,
                '5' => 5,
                '6' => 6,
                '7' => 7,
                '8' => 8,
                '9' => 9,
                _ => 0,
            };
            tmp.push(d);
        }
        tmp
    };

    let sorted = {
        let mut tmp = v.clone();
        tmp.sort_by(|a, b| b.cmp(a));
        tmp
    };

    let i = {
        let mut tmp = 0;
        while v[tmp] == sorted[tmp] {
            tmp += 1;
            if tmp == v.len() { break; }
        }
        tmp
    };

    if i < v.len() {
        let j = {
            let mut tmp = v.len() - 1;
            loop {
                if v[tmp] == sorted[i] { break; }
                tmp -= 1;
            }
            tmp
        };
    
        // swap
        v[i] ^= v[j];
        v[j] ^= v[i];
        v[i] ^= v[j];    
    }

    let ans = v.iter().fold(0, |acc, e| acc * 10 + e);

    println!("{}", ans);
}
0