競技プログラミングの鉄則    次の問題へ    前の問題へ

A11 Binary Search 1


問題へのリンク


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("15 47");
            WillReturn.Add("11 13 17 19 23 29 31 37 41 43 47 53 59 61 67");
            //11
        }
        else if (InputPattern == "Input2") {
            WillReturn.Add("10 80");
            WillReturn.Add("10 20 30 40 50 60 70 80 90 100");
            //8
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    static void Main()
    {
        List<string> InputList = GetInputList();
        int[] wkArr = InputList[0].Split(' ').Select(pX => int.Parse(pX)).ToArray();
        int X = wkArr[1];

        int[] AArr = InputList[1].Split(' ').Select(pX => int.Parse(pX)).ToArray();

        int Answer = Array.BinarySearch(AArr, X);
        Console.WriteLine(Answer + 1);
    }
}


解説

Array.BinarySearchを使ってます。