课后作业08 -- 二分查找法 (课后修改)
2015-08-10 23:17
369 查看
int[] arr1 = new int[] { 1, 3, 4, 5, 6, 9, 13, 25, 36, 45, 67 ,79}; foreach (int i in arr1) Console.Write(i + " ");//遍历数组 Console.WriteLine(" \n数组长度为:" + arr1.Length + "\n请输入要查找的数字:"); int num; try { num = Convert.ToInt32(Console.ReadLine()); } catch(Exception ex) { Console.WriteLine("输入错误!! \n错误信息是:" + ex.Message); Console.ReadLine(); return; } if (arr1.Contains(num))//判断数组是否包含要查找的数 { int a = 0 , b = arr1.Length -1 ,c ; while (a <= b)//条件判断中含有a = b ,可以有效查找两头 防止死循环 { c = (a + b) / 2; if (arr1[c] == num) { Console.WriteLine("您要找的数字是数组第" + (c + 1) +"位。"); break; } else if (arr1[c] > num) b = c - 1;//已经判断中间位置不等于要查找的数 可以多移一位增加效率 else a = c + 1; } } else Console.WriteLine("您要查找的数字不在数组内!"); Console.ReadLine();
相关文章推荐
- 【LeetCode】100 - Same Tree
- 第8章 用户模式下的线程同步(1)_Interlocked系列函数
- 【转】Java垃圾收集器
- 检索 04 --Stack栈 Queue队列 Hashtable哈希表
- COJ 0047 20702最大乘积
- 我为什么要加班?
- .Net分页实现
- oracle 11g数据库安全加固注意事项
- 利用Android的UXSS漏洞完成一次XSS攻击
- HDU 3639 SCC Hawk-and-Chicken
- grep命令
- iostransitiontranslate闪屏问题总结
- VIM文本编辑器
- 第80讲:List的泛型分析以及::类和Nil对象
- hdu5354 Bipartite Graph
- 文件系统
- JS encode decode
- Scala 深入浅出实战经典 第53讲:Scala中结构类型实战详解
- 如何用pdfbox-app-1.8.10.jar批处理将pdf文档转换成text文档
- IEF could not decode Chinese character in IE history well