HDU 1303 Doubles

来源:互联网 发布:java 深度遍历 编辑:程序博客网 时间:2024/06/06 19:41

http://acm.hdu.edu.cn/showproblem.php?pid=1303

 

Doubles

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


Problem Description
As part of an arithmetic competencyprogram, your students will be given randomly generated lists offrom 2 to 15 unique positive integers and asked to determine howmany 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 programshould be able to scan the lists and output the correct answer foreach 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 18is twice 9.


 

Input
The input file will consist of one ormore lists of numbers. There will be one list of numbers per line.Each list will contain from 2 to 15 unique positive integers. Nointeger will be larger than 99. Each line will be terminated withthe integer 0, which is not considered part of the list. A linewith the single number -1 will mark the end of the file. Theexample input below shows 3 separate lists. Some lists may notcontain any doubles.


 

Output
The output will consist of one line perinput list, containing a count of the items that are double someother item.


 

Sample Input
1 4 3 2 9 718 22 0 2 4 8 10 0 7 5 11 13 1 3 0 -1


 

Sample Output
3 2 0


 

Source
Mid-Central USA 2003


 

Recommend
Eddy
 
题目大意:一组数,以0结尾。寻找这组数当中有几对2倍数。也就是说,有几对数是2倍的关系。
分析:我又想起了哈希算法,总是能把问题很简单的处理掉。
代码如下:
#include<iostream>
using namespace std;  
int main()
{
 int n,i,count;
 int x[200];
 while(cin>>n)
 {
  if(n==-1) break;
  memset(x,0,sizeof(x));
  x[n]++;
  x[2*n]++;
  while(n)
  {
   cin>>n;
   x[n]++;
   x[2*n]++;
  }
  count=0;
  for(i=1;i<200;i++)
   if(x[i]>=2)
    count++;
   cout<<count<<endl;
 }
 return 0;
}
 
原创粉丝点击