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. SG函数入门&&HDU 1848

    SG函数 sg[i]为0表示i节点先手必败. 首先定义mex(minimal excludant)运算,这是施加于一个集合的运算,表示最小的不属于这个集合的非负整数.例如mex{0,1,2,4}=3. ...

  2. nodejs 静态资源服务与接口代理跨域

    首先需要 npm install express 和 npm install request 代码如下: const express = require('express'); const path ...

  3. PAT 乙级 1048

    题目 题目地址:PAT 乙级 1048 思路 这道题坑的地方在于:即使B的长度小于A,仍然要对B补齐,也就是说最终结果的长度取决于A和B中长度更长的那一项:即只要A.B长度不一致,就要对短的一个进行补 ...

  4. numpy学习(一)

    numpy数据类型 # numpy创建对象,numpy创建的对象是n阶矩阵,类似python中列表的嵌套 nd = np.array([[1,2,3,4,5],[2,3,4,6,5]])nd 结果: ...

  5. 腾讯云Ubuntu服务器修改root密码

    1.修改root密码 执行以下命令,按照提示修改密码 sudo passwd root 2.修改ssh配置 执行以下命令 sudo vi /etc/ssh/sshd_config 找到 PermitR ...

  6. RSA与AES实现数据加密传输

    RSA.AES简介 RSA:非对称加密,需要提前生成两个密钥(一对的),通过其中一个密钥加密后的数据,只有另一个密钥能解密.通常这两个密钥中有一个会暴漏出来,即对外公开的,这个密钥称为“公钥”,反之另 ...

  7. DNS预解析 dns-prefetch

    1.DNS 是什么? Domain Name System,域名系统,作为域名和IP地址相互映射的一个分布式数据库. DNS大家都懂,那么浏览器访问域名的时候,是需要去解析一次DNS,也就是把域名 g ...

  8. Python PycURL的安装使用

    PycURL中文简介:https://blog.csdn.net/qq_41185868/article/details/80487014 PycURL英文简介(如下):http://pycurl.i ...

  9. 第十四届华中科技大学程序设计竞赛决赛同步赛 Beautiful Land

    It’s universally acknowledged that there’re innumerable trees in the campus of HUST.Now HUST got a b ...

  10. selenium2基本控件介绍及其代码

    输入框:input  表现形式:      1.在html中一般为:<input id="user" type="text"> 主要操作:    ...