登机门|登机门CS 2008 |问题84

考虑下面的C程序,它试图使用二进制搜索来定位数组y[]中的元素X。这个程序是错误的。

null

1.   f( int Y[10], int x) {
2. int i, j, k;
3.     i = 0; j = 9;
4. do {
5.             k =  (i + j) /2;
6. if ( Y[k] < x)  i = k; else j = k;
7.         } while (Y[k] != x && i < j);
8. if (Y[k] == x) printf ( "x is in the array " ) ;
9. else printf ( " x is not in the array " ) ;
10. }


程序在Y和x的以下哪项内容上失败? (A) Y是[12345678910],x<10 (B) Y是[1 3 5 7 9 11 13 15 17 19],x<1 (C) Y是[2],x>2 (D) Y是[2 4 6 8 10 12 14 16 18 20],2 答复: (C) 说明: 见本报告问题3 https://www.geeksforgeeks.org/data-structures-and-algorithms-set-21/ 这个问题的小测验

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享