結果

問題 No.48 ロボットの操縦
ユーザー yo-kondo
提出日時 2019-03-03 16:16:42
言語 Rust
(1.83.0 + proconio)
結果
RE  
実行時間 -
コード長 1,336 bytes
コンパイル時間 12,419 ms
コンパイル使用メモリ 400,932 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-23 13:15:19
合計ジャッジ時間 13,493 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other RE * 25
権限があれば一括ダウンロードができます

ソースコード

diff #

use std::io::stdin;

/// エントリポイント
fn main() {
    let in1 = read_line();
    let in2 = read_line();
    let in3 = read_line();
    println!("{}", count_order(in1, in2, in3));
}

/// 移動先に到達するまでに必要な最小の命令回数を計算します。
///
/// ## arguments
/// * `east_west` - 目的地の東西方向
/// * `north_south` - 目的地の南北方向
/// * `max_distance` - ロボットが1命令につき前進することができる最大の距離
fn count_order(east_west: String, north_south: String, max_distance: String) -> i32 {
    let mut num = 0;
    let ew: f64 = east_west.parse().unwrap();
    let ns: f64 = north_south.parse().unwrap();
    let md: f64 = max_distance.parse().unwrap();

    // 北
    if ns > 0.0 {
        num += (ns / md).abs().ceil() as i32;
    }

    // 東西
    if ew != 0.0 {
        num += 1;
        num += (ew / md).abs().ceil() as i32;
    }

    // 北から南へ向く場合、いったん東西へ向く
    if ew == 0.0 && ns < 0.0 {
        num += 1;
    }

    // 南
    if ns < 0.0 {
        num += 1;
        num += (ns / md).abs().ceil() as i32;
    }

    num
}

/// 標準入力から1行取得して返します。
fn read_line() -> String {
    let mut input = String::new();
    stdin().read_line(&mut input).unwrap();
    input
}
0