首页 > 其他 > 详细

zoj 1760 查找

时间:2015-11-14 06:12:11      阅读:303      评论:0      收藏:0      [点我收藏+]

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 }

 

zoj 1760 查找

原文:http://www.cnblogs.com/qlky/p/4963645.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!