You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

66 lines
1.2 KiB

1 year ago
#include<iostream>
using namespace std;
#define OK 1
#define ERROR 0
#define MAXSIZE 100
typedef int KeyType;
typedef struct
{
int elem[MAXSIZE];
int length;
}SSTable; //<2F><><EFBFBD><EFBFBD>һ<EFBFBD><D2BB>˳<EFBFBD><CBB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><D4AA>
int Search_Bin(SSTable ST,KeyType key)
{
int low,high,mid;
low=1;
high=ST.length;
while(low<=high)
{
mid=(low+high)/2; //<2F>۰<DBB0><EBA3AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ƚϵ<C8BD>Ԫ<EFBFBD><D4AA>
cout<<" "<<ST.elem[mid];
if(ST.elem[mid]==key)
return mid;
else if(ST.elem[mid]>key) //<2F><><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC>ִ<EFBFBD><D6B4>ڲ<EFBFBD><DAB2>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD>С<EFBFBD><D0A1><EFBFBD>ƶ<EFBFBD>
high=mid-1;
else if(ST.elem[mid]<key) //<2F><><EFBFBD><EFBFBD><EFBFBD>ؼ<EFBFBD><D8BC>ִ<EFBFBD><D6B4>ڲ<EFBFBD><DAB2>ҵ<EFBFBD><D2B5><EFBFBD><EFBFBD>֣<EFBFBD><D6A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ƶ<EFBFBD>
low=mid+1;
}
return -1;
}
int main()
{
SSTable ST;
KeyType n;
int a;
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>ȣ<EFBFBD>"<<endl;
cin>>ST.length;
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>"<<endl;
for(int i=1;i<=ST.length;i++)
{
cin>>ST.elem[i];
}
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>"<<endl;
cin>>n;
cout<<"<EFBFBD>Ƚϵ<EFBFBD>Ԫ<EFBFBD><EFBFBD><EFBFBD>У<EFBFBD>";
a=Search_Bin(ST,n);
if(a>=0)
cout<<"\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD>ص<EFBFBD><EFBFBD>±<EFBFBD><EFBFBD>ǣ<EFBFBD>"<<a<<endl;
else
cout<<"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>δ<EFBFBD>ҵ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ҵ<EFBFBD>Ԫ<EFBFBD>أ<EFBFBD>"<<endl;
}
/*
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ij<EFBFBD><EFBFBD>ȣ<EFBFBD>
11
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ԫ<EFBFBD><EFBFBD>
05 13 19 21 37 56 64 75 80 88 92
<EFBFBD><EFBFBD><EFBFBD><EFBFBD><E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