Doubles

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 4266    Accepted Submission(s): 2945

Problem Description
As part of an arithmetic competency program, your students will be given randomly generated lists of from 2 to 15 unique positive integers and asked to determine how many items in each list are twice some other item in the same list.
You will need a program to help you with the grading. This program should be able to scan the lists and output the correct answer for each one. For example, given the list
1 4 3 2 9 7 18 22

your program should answer 3, as 2 is twice 1, 4 is twice 2, and 18 is twice 9.

 
Input
The input file will consist of one or more lists of numbers. There will be one list of numbers per line. Each list will contain from 2 to 15 unique positive integers. No integer will be larger than 99. Each line will be terminated
with the integer 0, which is not considered part of the list. A line with the single number -1 will mark the end of the file. The example input below shows 3 separate lists. Some lists may not contain any doubles.
 
Output
The output will consist of one line per input list, containing a count of the items that are double some other item.
 
Sample Input
1 4 3 2 9 7 18 22 0
2 4 8 10 0
7 5 11 13 1 3 0
-1
 
Sample Output
3
2
0
题目简单,就是注意下数组何时进行创建并初始化、输入即可。既然今天学了二分查找,那就拿这道题试试手吧。最基础的二分查找(用二分一般得是有序的比如sort过的序列,不然难办)
代码:
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
bool sea(const int a[],const int &len,const int &n)
{
int low=0,high=len-1,mid;//定义数组的开始,结尾,以及要用到的mid伪下标中值
while (low<=high)
{
mid=(high+low)/2;
if(a[mid]==n)
return true;找到直接返回true
else if(a[mid]<n)
{
low=mid+1;
}
else if(a[mid]>n)
{
high=mid-1;
}
}
return false;//上面没有进行返回说明没找到,返回false
}
int main(void)
{
int t,sum,j,k,i;
while (cin>>t&&t!=-1)
{
i=0;
int list[20000]={0};
list[i++]=t;
while (cin>>t&&t)
{
list[i++]=t;
}
sort(list,list+i);
sum=0;
for (j=0; j<i; j++)
{
sum+=sea(list,i,2*list[j]);//用布尔值计算挺方便
}
cout<<sum<<endl;
i=0;
}
return 0;
}
之前想用vector的二分搜索函数的,调试半天发现怎么连例子数据都对不上...郁闷半天原来if后面多了个分号
#include<iostream>
#include<vector>
#include<algorithm>
#include<numeric>
using namespace std;
int main(void)
{
int t,sum,temp;
vector<int>list;
while (cin>>t&&t!=-1)
{
vector<int>::iterator it;
if(t!=0)
{
list.push_back(t);
}
else
{
sum=0;
sort(list.begin(),list.end());
for (it=list.begin(); it!=list.end(); it++)
{
temp=2*(*it);
if(binary_search(list.begin(),list.end(),temp))//之前这里多了个分号难怪错的....
{
sum++;
}
}
cout<<sum<<endl;
list.clear();
}
}
return 0;
}

HDU——1303Doubles(水题,试手二分查找)的更多相关文章

  1. HDU-1042-N!(Java大法好 &amp;&amp; HDU大数水题)

    N! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) Total Subm ...

  2. HDU 5391 水题。

    E - 5 Time Limit:1500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  3. hdu 1544 水题

    水题 /* * Author : ben */ #include <cstdio> #include <cstdlib> #include <cstring> #i ...

  4. HDU排序水题

    1040水题; These days, I am thinking about a question, how can I get a problem as easy as A+B? It is fa ...

  5. hdu 2710 水题

    题意:判断一些数里有最大因子的数 水题,省赛即将临近,高效的代码风格需要养成,为了简化代码,以后可能会更多的使用宏定义,但是通常也只是快速拿下第一道水题,涨自信.大部分的代码还是普通的形式,实际上能简 ...

  6. <每日一题>题目6:二分查找

    #二分查找 ''' 1.end问题 2.44对应的end<start 找不到情况 3.返回值递归的情况 4,611,aim太大的情况 ''' l = [2,3,5,10,15,16,18,22, ...

  7. Dijkstra算法---HDU 2544 水题(模板)

    /* 对于只会弗洛伊德的我,迪杰斯特拉有点不是很理解,后来发现这主要用于单源最短路,稍稍明白了点,不过还是很菜,这里只是用了邻接矩阵 套模板,对于邻接表暂时还,,,没做题,后续再更新.现将这题贴上,应 ...

  8. hdu 4190 Distributing Ballot Boxes(贪心+二分查找)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4190 Distributing Ballot Boxes Time Limit: 20000/1000 ...

  9. hdu 5162(水题)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5162 题解:看了半天以为测试用例写错了.这题玩文字游戏.它问的是当前第i名是原数组中的第几个. #i ...

随机推荐

  1. 卓越管理的实践技巧(1)如何进行有效的指导 Guidelines for Effective Coaching

    Guidelines for Effective Coaching 前文卓越管理的秘密(Behind Closed Doors)最后一部分提到了总结的13条卓越管理的实践技巧并列出了所有实践技巧名称的 ...

  2. java基础—异常处理

    一.异常的概念 异常指的是运行期出现的错误,也就是当程序开始执行以后执行期出现的错误.出现错误时观察错误的名字和行号最为重要.

  3. 正确适配苹果ATS审核要求的姿势

    首先,ATS的技术行为不会有任何变化(除了新增两个字段NSAllowsArbitraryLoadsInWebContent和NSRequiresCertificateTransparency,也就是更 ...

  4. 音频框架TheAmazingAudioEngine实现音效

    TheAmazingAudioEngine是Michael Tyson开源的iOS第三方音频框架.很多音频类APP应用这个框架作开发. 应用这个框架,可以比较方便地实现iOS音频开发中的各种音效的实现 ...

  5. 转 Keras 保存与加载网络模型

    https://blog.csdn.net/qq_28413479/article/details/77367665

  6. 【离线 撤销并查集 线段树分治】bzoj1018: [SHOI2008]堵塞的交通traffic

    本题可化成更一般的问题:离线动态图询问连通性 当然可以利用它的特殊性质,采用在线线段树维护一些标记的方法 Description 有一天,由于某种穿越现象作用,你来到了传说中的小人国.小人国的布局非常 ...

  7. idea 关于高亮显示与选中字符串相同的内容踩过的坑

    由 Eclipse 切换到 idea 遇到了很多不熟的地方和踩过的坑,下面记录 idea 关于高亮显示与选中字符串相同的内容踩过的坑. IDEA 2017.2.1 版本修改 Identifier un ...

  8. Applied Nonparametric Statistics-lec5

    今天继续two-sample test Ref: https://onlinecourses.science.psu.edu/stat464/print/book/export/html/6 Mann ...

  9. solr7.7.1完整教程

    安装 上传solr-7.7.1.tgz至服务器 opt文件加下 解压 tar -zxvf solr-7.7.1.tgz 运行 进入到加压后的文件夹/opt/solr-7.7.1,执行一下命令启动sol ...

  10. 学习ucosii要用到的几本书

    转自:http://bbs.elecfans.com/jishu_551275_1_1.html   1.嵌入式实时操作系统μC/OS-II(第2版)  邵贝贝 等译 北京航空航天大学出版社     ...