Problem M-13 Doubles
来源:互联网 发布:mysql create database 编辑:程序博客网 时间:2024/06/06 13:56
Description
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()-- 移除全部元素,将整个容器清空。
- Problem M-13 Doubles
- ZOJ Problem Set - 1760 Doubles
- ZOJ Problem Set - 1760 Doubles
- Doubles
- Doubles
- Doubles
- Doubles
- Doubles
- Doubles
- Doubles
- Problem M
- Problem M
- Problem M
- Problem M
- Problem M
- Problem M
- Problem M
- Problem-M
- pip安装与镜像源设置
- 云服务替代私有服务器的十个原因
- mac下的redis安装与配置
- 不用+的加法
- CentOS安装SecureCRT和VMTools实现远程连接
- Problem M-13 Doubles
- SQL备忘
- JDK、JRE、JVM三者间的关系
- Linux 网络编程——UDP编程
- 如何计算空间一条直线外一点到这条直线的垂足点坐标
- adb shell 命令详解
- Hibernate 查询MatchMode的四种模式
- Web攻防系列教程之跨站脚本攻击和防范技巧详解
- sklearn数据标准化