結果

問題 No.48 ロボットの操縦
ユーザー yo-kondo
提出日時 2019-03-03 16:22:52
言語 Rust
(1.83.0 + proconio)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,357 bytes
コンパイル時間 13,936 ms
コンパイル使用メモリ 402,512 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-06-23 13:15:43
合計ジャッジ時間 15,033 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 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.trim().parse().unwrap();
    let ns: f64 = north_south.trim().parse().unwrap();
    let md: f64 = max_distance.trim().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