http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=760
撸了个二分查找
1 #include<iostream> 2 #include<cmath> 3 #include<iomanip> 4 #include<algorithm> 5 using namespace std; 6 7 int bs(int a[],int n,int len) 8 { 9 int lo = 0,hi = len-1,mid = (lo+hi)/2; 10 while(lo<=hi) 11 { 12 if(n<a[mid]) 13 { 14 hi = mid -1; 15 mid = (lo+hi)/2; 16 } 17 else if(n>a[mid]) 18 { 19 lo = mid +1; 20 mid = (lo+hi)/2; 21 } 22 else 23 return mid; 24 } 25 if(lo>hi) 26 return -1; 27 } 28 29 30 int main() 31 { 32 int a[10000]; 33 int x; 34 while(cin>>x && x!=-1) 35 { 36 int i = 1; 37 a[0] = x; 38 while(cin>>a[i]) 39 { 40 if(a[i]==0) 41 break; 42 i++; 43 } 44 sort(a,a+i); 45 int count = 0; 46 for(int j = i-1;j>=0;j--) 47 { 48 if(bs(a,a[j]*2,i)!=-1) 49 { 50 count++; 51 } 52 } 53 cout<<count<<endl; 54 55 56 } 57 }
时间: 2024-10-12 23:44:36