#include
using std::cout;
using std::cin;
using namespace std;
class chazhaobiao
{
private:
int *date;
int total;
public:
chazhaobiao(int a[],int n)//n是共有多少個元素
{
date=a;
total=n;
}
int search_date(int m)//m是要查詢的數據,要查找需要先排序
{
int after[total];
for(int i=0;i<total;i++)
after[i]=date[i];
int i, j, t, k;
for(i = 0; i < total-1; i++)
{
k = i;
for(j = i+1; j < total; j++)
{
if(after[j] < after[k])
k = j;
}
if(k != i)
{
t = after[k];
after[k] = after[i];
after[i] = t;
}
}
int right=total,left=0,middle=(right+left)/2;
int pp=0;
while(pp==1||pp==-1)
{
if(m==after[middle])
{
cout<<middle;
pp=1;
}
else if(m<after[middle])
{
right=middle;
middle=(right+left)/2;
}
else {
left=middle;
middle=(right+left)/2;
}
if(middle<0||middle>total)
{
cout<<"查詢失敗";
pp=-1;
}
}
}
};
int main()
{
cout << "請輸入元素個數:";
int mumber;
std::cin>>mumber;
int s[mumber];
cout<<"請輸入元素:";
for(int i=0;i
cin>>s[i];
chazhaobiao b1(s,mumber);
while(true)
{
cout<<"輸入要查詢的元素:";
int temp=0;
cin>>temp;
temp=b1.search_date(temp);
cout<<temp<<endl;
break;
}
return 0;
}