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. 修改deeplabv3的test的输出的label颜色

    deeplab.py是拿来做test的,其中的postprecess函数中的palette = pascal_palette_invert()是给每个类别加颜色 这个是通过import utils获得 ...

  2. 爬虫1_python2

    # -*- coding: UTF-8 -*- # python2爬虫 import urllib f = urllib.urlopen("http://www.itcast.cn/&quo ...

  3. Bootstrap历练实例:默认的面板(Panels)

    Bootstrap 面板(Panels) 本章将讲解 Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素 ...

  4. linux命令行调试邮件服务器

    linux命令行调试邮件服务器 1. Linux客户端调试邮件过程 [root@mxtest ~]# telnet mail.xx.com 25 Trying 172.16.236.103... Co ...

  5. Yii2应用的运行过程

    每一个框架都有一个入口脚本,Yii2也不例外.一般来说,对于Web应用的入口脚本是YiiBasePath/frontend/web目录下的index.php. 先观察这个文件: <?php de ...

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

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

  7. 【wqs二分】HHHOJ#15. 赤

    这个wqs二分并不熟练…… 题目描述 #15. 赤 题目分析 两维都用wqs二分,其他没有什么特殊之处. 重点在于,wqs二分还原最优解的时候,增量是强制给的k. #include<bits/s ...

  8. LeetCode之Weekly Contest 90

    LeetCode第90场周赛记录 第一题:亲密字符串 问题: 给定两个由小写字母构成的字符串 A 和 B ,只要我们可以通过交换 A 中的两个字母得到与 B 相等的结果,就返回 true :否则返回  ...

  9. Python基础——文件操作

    写文件 writefile %%writefile ./data/testFile.txt hello python jin tian tian qi bu cuo open覆盖 txt=open(' ...

  10. python模块之shutil和zipfile

    shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length])将文件内容拷贝到另一个文件中 import shutil s ...