标签:
class Program { static int Search(int a ,int b,int s,int[] arr) { while (a <= b) { int c = (a + b )/2; if (arr[c] == s) { return c; } else if (arr[c] > s) b = c - 1; else a = c + 1; } return -1; } static void Main(string[] args) { Console.WriteLine("数组为:"); int[] arr1 = new int []{1,3,4,5,6,9,13,15,18,29,34,39,40,45,48,57,59,64}; foreach (int i in arr1) Console.Write(i + " "); Console.WriteLine("\n您要查找的数:"); int s = Convert.ToInt32(Console.ReadLine()); if (Search(0, arr1.Length - 1, s,arr1) >= 0) { Console.WriteLine("您要查找的数为第" + (Search(0, arr1.Length - 1, s,arr1) + 1) + "位!"); } else Console.WriteLine("您要查找的数字不存在!"); Console.ReadLine(); } }
标签:
原文地址:http://www.cnblogs.com/whytohow/p/4725834.html