1.1.23

上次更新:2019-04-17
发现了题解错误/代码缺陷/排版问题?请点这里:如何:提交反馈

解答

在主函数里做一下判断就可以了,加号则输出所有找不到的值,减号则相反。

代码

static void Main(string[] args)
{
    //从largeW.txt中读取数据
    string[] whiteList = File.ReadAllLines("largeW.txt");
    int[] WhiteList = new int[whiteList.Length];

    for (int i = 0; i < whiteList.Length; ++i)
    {
        WhiteList[i] = int.Parse(whiteList[i]);
    }

    Array.Sort<int>(WhiteList);

    Console.WriteLine("Type the numbers you want to query: ");
    //输入样例:5 824524 478510 387221
    string input = Console.ReadLine();
    int[] Query = new int[input.Split(' ').Length];
    for (int i = 0; i < Query.Length; ++i)
    {
        Query[i] = int.Parse(input.Split(' ')[i]);
    }

    Console.WriteLine("Type '+' to get the numbers that not in the whitelist," + 
                      "'-' to get the numbers that in the whitelist.");
    char operation = Console.ReadLine()[0];

    foreach (int n in Query)
    {
        if (rank(n, WhiteList) == -1)
        {
            if (operation == '+')
            {
                Console.WriteLine(n);
            }
        }
        else if (operation == '-')
        {
            Console.WriteLine(n);
        }
    }
}

//重载方法,用于启动二分查找
public static int rank(int key, int[] a)
{
    return rank(key, a, 0, a.Length - 1);
}

//二分查找
public static int rank(int key, int[] a, int lo, int hi)
{

    if (lo > hi)
    {
        return -1;
    }

    int mid = lo + (hi - lo) / 2;

    if (key < a[mid])
    {
        return rank(key, a, lo, mid - 1);
    }
    else if (key > a[mid])
    {
        return rank(key, a, mid + 1, hi);
    }
    else
    {
        return mid;
    }
}

另请参阅

LargeW.txt

上一题 下一题