标签:\n clu pos 保存 二分查找 col typedef put size
1 #include <stdio.h> 2 #include <stdlib.h> 3 #define MAXSIZE 10 4 #define NotFound 0 5 typedef int ElementType; 6 typedef int Position; 7 typedef struct LNode *List; 8 struct LNode { 9 ElementType Data[MAXSIZE]; 10 Position Last; /* 保存线性表中最后一个元素的位置 */ 11 }; 12 List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */ 13 Position BinarySearch( List L, ElementType X ); 14 int main() 15 { 16 List L; 17 ElementType X; 18 Position P; 19 L = ReadInput(); 20 scanf("%d", &X); 21 P = BinarySearch( L, X ); 22 printf("%d\n", P); 23 return 0; 24 } 25 26 /* 你的代码将被嵌在这里 */ 27 List ReadInput(){ 28 List PtrL; 29 int N; 30 scanf("%d", &N); 31 for(int i = 1; i <= N; i++){ 32 scanf("%d",&PtrL->Data[i]); 33 } 34 PtrL->Last = N; 35 return PtrL; 36 } 37 Position BinarySearch( List L, ElementType X ){ 38 int Left = 1, Right = L->Last, mid; 39 while(Right - Left >= 0 ){ 40 mid = (Left + Right) / 2; 41 if(L->Data[mid] > X){ 42 Right = mid - 1; 43 }else if(L->Data[mid] == X){ 44 return mid; 45 }else{ 46 Left = mid + 1; 47 } 48 } 49 return NotFound; 50 }
标签:\n clu pos 保存 二分查找 col typedef put size
原文地址:https://www.cnblogs.com/zjsaipplp/p/10423626.html