AtCoderのABC    次のABCの問題へ    前のABCの問題へ

ABC153-D Caracal vs Monster


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("2");
            //3
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("4");
            //7
        }
        else if (InputPattern == "Input3") {
            WillReturn.Add("1000000000000");
            //1099511627775
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        long H = long.Parse(InputList[0]);

        Console.WriteLine(Solve(H));
    }

    static long Solve(long pHP)
    {
        if (pHP == 1) return 1;
        return Solve(pHP / 2) * 2 + 1;
    }
}


解説

再帰呼び出しを使ってます。