标签:ptr inpu while not == def type print 二分查找
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 10
#define NotFound 0
typedef int ElementType;
typedef int Position;
typedef struct LNode *List;
struct LNode {
ElementType Data[MAXSIZE];
Position Last; /* 保存线性表中最后一个元素的位置 */
};
List ReadInput(); /* 裁判实现,细节不表。元素从下标1开始存储 */
Position BinarySearch( List L, ElementType X );
int main()
{
List L;
ElementType X;
Position P;
L = ReadInput();
scanf("%d", &X);
P = BinarySearch( L, X );
printf("%d\n", P);
return 0;
}
/* 你的代码将被嵌在这里 */
List ReadInput(){
List PtrL;
int N;
scanf("%d", &N);
for(int i = 1; i <= N; i++){
scanf("%d",&PtrL->Data[i]);
}
PtrL->Last = N;
return PtrL;
}
Position BinarySearch( List L, ElementType X ){
int Left = 1, Right = L->Last, mid;
while(Right - Left >= 0 ){
mid = (Left + Right) / 2;
if(L->Data[mid] > X){
Right = mid - 1;
}else if(L->Data[mid] == X){
return mid;
}else{
Left = mid + 1;
}
}
return NotFound;
}
标签:ptr inpu while not == def type print 二分查找
原文地址:https://www.cnblogs.com/zjsaipplp/p/10424832.html