結果

問題 No.39 桁の数字を入れ替え
ユーザー frozenlibfrozenlib
提出日時 2018-06-15 18:40:59
言語 Rust
(1.77.0)
結果
AC  
実行時間 1 ms / 5,000 ms
コード長 1,541 bytes
コンパイル時間 1,271 ms
コンパイル使用メモリ 146,168 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-09-13 04:40:35
合計ジャッジ時間 1,713 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

use std::io::*;
use std::str::FromStr;
use utils::*;

pub fn main() {
    let i = stdin();
    let mut o = Vec::new();
    run(i.lock(), &mut o);
    stdout().write_all(&o).unwrap();
}

fn run<R: BufRead, W: Write>(i: R, o: &mut W) {
    let mut i = CpReader::new(i);
    let n = i.read::<usize>();

    writeln!(o, "{}", solve(n)).unwrap();
}
fn solve(mut n: usize) -> usize {
    let mut ns = Vec::new();
    let mut maxs = Vec::new();
    let mut max_val = 0;
    let mut max_i = 0;

    while n != 0 {
        let val = n % 10;
        if val > max_val {
            max_val = val;
            max_i = ns.len();
        }
        maxs.push((max_val, max_i));

        ns.push(val);
        n /= 10;
    }
    for i in (1..ns.len()).rev() {
        if maxs[i - 1].0 > ns[i] {
            ns.swap(i, maxs[i - 1].1);
            break;
        }
    }
    let mut b = 1;
    let mut result = 0;
    for &n in &ns {
        result += n * b;
        b *= 10;
    }
    result
}

mod utils {
    use super::*;

    pub struct CpReader<R: BufRead> {
        r: R,
        s: String,
    }
    impl<R: BufRead> CpReader<R> {
        pub fn new(r: R) -> Self {
            CpReader {
                r: r,
                s: String::new(),
            }
        }
        pub fn read_line(&mut self) -> &str {
            self.s.clear();
            self.r.read_line(&mut self.s).unwrap();
            self.s.trim()
        }

        pub fn read<T: FromStr>(&mut self) -> T {
            self.read_line().parse().ok().unwrap()
        }
    }
}
0