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. UVA 11491 Erasing and Winning 奖品的价值 (贪心)

    题意:给你一个n位整数,让你删掉d个数字,剩下的数字要尽量大. 题解:因为最后数字位数是确定的,而且低位数字对答案的贡献是一定不及高位数字的,所以优先选择选最大且最靠左边的数字,但是有一个限制,选完这 ...

  2. Android(java)学习笔记113:Activity的生命周期

    1.首先来一张生命周期的总图: onCreate():创建Acitivity界面       onStart():让上面创建的界面可见              onResume():让上面创建的界面 ...

  3. UI的组织形式

    UI的组织形式是树状结构: 根据层次的不同分为叶子节点和干节点. 叶子节点负责简单的信息展示. 复杂的主干复杂叶子节点的组织和整体展示. http://www.cnblogs.com/feng9exe ...

  4. [神经网络]一步一步使用Mobile-Net完成视觉识别(四)

    1.环境配置 2.数据集获取 3.训练集获取 4.训练 5.调用测试训练结果 6.代码讲解 本文是第四篇,下载预训练模型并训练自己的数据集. 前面我们配置好了labelmap,下面我们开始下载训练好的 ...

  5. async/await的使用以及注意事项

    使用 async / await, 搭配 promise, 可以通过编写形似同步的代码来处理异步流程, 提高代码的简洁性和可读性. 本文介绍 async / await 的基本用法和一些注意事项. a ...

  6. python_100_静态方法

    class Dog(object): def __init__(self,name): self.name=name @staticmethod#实际上跟类没什么关系了 def eat():#def ...

  7. 安装python3.6 pip3 flake8

    apt-get install python3-pip # Python3 ➜  ~ pip3 -V                                             pip 9 ...

  8. QT5:第一章 初始化

    一.简介 二.新建项目 在项目Application中: QT Widgets Application(桌面QT应用) QT Console Application(控制台QT应用) QT for P ...

  9. JAVA - Annotation 注解 入门

    Java注解提供了关于代码的一些信息,但并不直接作用于它所注解的代码内容.在这个教程当中,我们将学习Java的注解,如何定制注解,注解的使用以及如何通过反射解析注解. Java1.5引入了注解,当前许 ...

  10. 关于lua 5.3 服务端热更新流程

    脚本的热更新的流程都大同小异, 第一步先保存旧代码的块的数据, 第二部加载新的代码块,第三步将旧代码块的局部和全局数据拷贝到新代码块的对应的 变量中. 在服务器热更新中,主要考虑热更的内容是什么, 一 ...