結果

問題 No.26 シャッフルゲーム
ユーザー yo-kondoyo-kondo
提出日時 2019-01-02 10:19:20
言語 Rust
(1.77.0)
結果
RE  
実行時間 -
コード長 2,414 bytes
コンパイル時間 15,628 ms
コンパイル使用メモリ 377,132 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-28 22:01:22
合計ジャッジ時間 14,679 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 RE -
testcase_01 RE -
testcase_02 RE -
testcase_03 RE -
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 RE -
testcase_08 RE -
testcase_09 RE -
権限があれば一括ダウンロードができます
コンパイルメッセージ
warning: field `shuffle_count` is never read
  --> src/main.rs:15:5
   |
11 | struct InputData {
   |        --------- field in this struct
...
15 |     shuffle_count: i32,
   |     ^^^^^^^^^^^^^
   |
   = note: `InputData` has a derived impl for the trait `Debug`, but this is intentionally ignored during dead code analysis
   = note: `#[warn(dead_code)]` on by default

ソースコード

diff #

//! No.26 シャッフルゲーム
//! https://yukicoder.me/problems/no/26

use std::io;

#[cfg(test)]
mod test;

/// 入力データ
#[derive(Debug)]
struct InputData {
    /// 最初に○印が付いているカップの位置
    first_position: i32,
    /// シャッフル回数
    shuffle_count: i32,
    /// シャッフルした位置
    shuffle_indices: Vec<[i32; 2]>,
}

/// エントリポイント
fn main() {
    let input = input_data();
    println!("{}", position_number(input));
}

/// ○印がついているカップの位置を返します。
fn position_number(input: InputData) -> i32 {
    // カップの位置情報(true:○、false:×)
    let mut correct_answer: [bool; 3] = [false, false, false];

    // 初期値
    correct_answer[(input.first_position - 1) as usize] = true;

    for index in input.shuffle_indices {
        let i = (index[0] - 1) as usize;
        let j = (index[1] - 1) as usize;

        let temp = correct_answer[i];
        correct_answer[i] = correct_answer[j];
        correct_answer[j] = temp;

        // 配列のswapは&mutでムーブされてしまうため、エラーが発生する。
        // error[E0499]: cannot borrow `correct_answer[..]` as mutable more than once at a time
        // std::mem::swap(&mut correct_answer[i], &mut correct_answer[j]);
    }

    // 結果を探す
    for (i, ans) in correct_answer.iter().enumerate() {
        if *ans {
            return i as i32 + 1i32;
        }
    }

    // ここには来ない
    0
}

/// 標準入力から文字列を取得します。
fn input_data() -> InputData {
    // 1行目
    let mut s1 = String::new();
    io::stdin().read_line(&mut s1).unwrap();
    let first_position: i32 = s1.trim().parse().unwrap();

    // 2行目
    let mut s2 = String::new();
    io::stdin().read_line(&mut s2).unwrap();
    let shuffle_count: i32 = s2.trim().parse().unwrap();

    // 3行目以降
    let mut shuffle_indices = Vec::new();
    for _i in 0..shuffle_count {
        let mut s3 = String::new();
        io::stdin().read_line(&mut s3).unwrap();

        let sp: Vec<&str> = s2.trim().split_whitespace().collect();
        let array: [i32; 2] = [
            sp[0].parse().unwrap(),
            sp[1].parse().unwrap(),
        ];
        shuffle_indices.push(array);
    }

    InputData {
        first_position,
        shuffle_count,
        shuffle_indices,
    }
}
0