标签:out 二分 int pac bsp binary sea bin 分治
#include <iostream> #include <algorithm> using namespace std; const int M = 10000; int x,n,i; int s[M]; int BinarySearch(int n,int s[],int x) { int low=0,high=n-1; while(low<=high) { int middle=(low+high)/2; if(x==s[middle]) { return middle; } else if(x>s[middle]) { low=middle+1; } else { high=middle-1; } } return -1; } int main() { while(cin>>n) { for(i=0; i<n; i++) { cin>>s[i]; } sort(s,s+n); for(i=0; i<n; i++) { cout<<s[i]<<" "; } cout<<endl; cin>>x; i=BinarySearch(n,s,x); if(i==-1) { cout<<"meiyou"<<endl; } else { cout<<"di"<<i+1<<"wei"<<endl; } } return 0; }
标签:out 二分 int pac bsp binary sea bin 分治
原文地址:https://www.cnblogs.com/xjyxp/p/11141008.html