Problem M-13 Doubles

来源:互联网 发布:mysql create database 编辑:程序博客网 时间:2024/06/06 13:56

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

题目介绍
输入一列或几列数字,判断每行某数是某数的二倍的个数,然后输出
解题思路

用集合来接收数字使其自动排序,然后通过查找2倍关系累加得到最后答案

源代码
#include<bits/stdc++.h>
using namespace std;
int main()
{
        set<int>s;
        set<int>::iterator it,itt;
        int sum=0;
        int n;
        while(cin>>n)
        {
                if(n==-1) break;
                if(n!=0)
                  s.insert(n);
                if(n==0)
                {
                        for(it=s.begin();it!=s.end();it++)
                        {
                                if(s.find(2*(*it))!=s.end())
                                        sum++;
                        }
                        cout<<sum<<endl;
                        sum=0;
                        s.clear();
                }


        }
        return 0;
}

set和multiset的区别在于set中的元素是唯一的,不能重复出现,但两者都会将输入的元素进行自动排序

头文件:#include <set>

定义:set<data_type> set_name;

  如:set<int> s;//默认由小到大排序

操作:

s.insert(elem)-- 安插一个elem副本,返回新元素位置。

s.erase(elem)-- 移除与elem元素相等的所有元素,返回被移除  的元素个数。

s.erase(pos)-- 移除迭代器pos所指位置上的元素,无返回值。

s.clear()-- 移除全部元素,将整个容器清空。



0 0