zoukankan      html  css  js  c++  java
  • c# 二分查找法(2分钟算法)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication14
    {
    class Program
    {
    static void Main(string[] args)
    {
    int sequence = 0;
    List<int> ints = new List<int>()
    {
    9,832,32,2,1,10,4

    };
    while (true)
    {
    Console.WriteLine("请输入要查找的值:");
    bool found = false;
    int a = int.Parse(Console.ReadLine());
    int mid = 0;
    int i = 0;
    int j = ints.Count - 1;
    while (i < j)
    {
    if (i == j && ints[i] != a)
    {
    break;

    }
    mid = (i + j) / 2;
    if (ints[mid] == a)
    {
    sequence = mid;
    found = true;
    break;
    }
    if (ints[mid] > a)
    {
    i = mid + 1;

    }
    else
    {
    j = mid - 1;

    }


    }
    if (found)
    {
    Console.WriteLine("找到:" + a + " 在序号" + (sequence+1));
    }
    else
    {
    Console.WriteLine("没找到。");
    }
    }
    Console.ReadKey();
    }
    }
    }

  • 相关阅读:
    自定义长时间定时器对象
    poj1326
    poj1323
    poj1218
    poj1298
    poj1276
    新年的第一场雪
    Java 语言学习总结
    假使时光能够倒转
    为了回家——春运3日战纪实
  • 原文地址:https://www.cnblogs.com/kexb/p/4941264.html
Copyright © 2011-2022 走看看