結果

問題 No.642 Two Operations No.1
ユーザー bluemeganebluemegane
提出日時 2018-09-09 11:03:00
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 62 ms / 2,000 ms
コード長 926 bytes
コンパイル時間 4,532 ms
コンパイル使用メモリ 104,100 KB
実行使用メモリ 24,328 KB
最終ジャッジ日時 2023-08-24 18:14:11
合計ジャッジ時間 4,832 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 60 ms
24,328 KB
testcase_01 AC 56 ms
20,272 KB
testcase_02 AC 58 ms
24,316 KB
testcase_03 AC 58 ms
22,320 KB
testcase_04 AC 58 ms
22,196 KB
testcase_05 AC 59 ms
22,380 KB
testcase_06 AC 61 ms
24,328 KB
testcase_07 AC 59 ms
22,484 KB
testcase_08 AC 58 ms
22,340 KB
testcase_09 AC 62 ms
22,272 KB
testcase_10 AC 59 ms
20,296 KB
testcase_11 AC 59 ms
22,392 KB
testcase_12 AC 58 ms
22,268 KB
testcase_13 AC 58 ms
22,420 KB
testcase_14 AC 58 ms
22,400 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System.Collections.Generic;
using System;

public class P
{
    public int a { get; set; }
    public int times { get; set; }
}

public class Hello
{
    public static void Main()
    {
        var n = int.Parse(Console.ReadLine().Trim());
        var ans = getA(n);
        Console.WriteLine(ans);
    }
    public static int getA(int n)
    {
        var hs = new HashSet<int>();
        var q = new Queue<P>();
        q.Enqueue(new P { a = n, times = 0 });
        while (true)
        {
            var w = q.Dequeue();
            if (w.a == 1) return w.times;
            if (w.a % 2 == 0)
            {
                if (hs.Add(w.a / 2)) q.Enqueue(new P { a = w.a / 2, times = w.times + 1 });
                if (hs.Add(w.a + 1)) q.Enqueue(new P { a = w.a + 1, times = w.times + 1 });
            }
            else if (hs.Add(w.a + 1)) q.Enqueue(new P { a = w.a + 1, times = w.times + 1 });
        }
    }
}
0