Problem M-13 Doubles
2017-03-17 09:02
288 查看
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()-- 移除全部元素,将整个容器清空。
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()-- 移除全部元素,将整个容器清空。
相关文章推荐
- ACM程序设计题目 Problem M-13
- 微信公众帐号开发教程第13篇-图文消息全攻略 (13)
- 3-【python13_day2】.md
- 0 1 1 2 3 5 8 13 21 34 求第N个, 用js实现
- 13、linux系统启动流程
- 34,UC(13)
- Leetcode(W13):718.Maximum Length of Repeated Subarray
- 13 Stream(续)
- python教程13--《控制语句之 if、while、for》
- Java Hour 13 集合基础
- 2018 年,你可以把论文投给这 13 场 AI 学术会议(附截稿日期)
- 2014 Jordan 4 Unc and jordan 13 black red of the Jordan4unc.com
- 第十九章 ALDS1_13_A:8 Queens Problem 八皇后问题
- 13.输入偏置均衡电阻——你确定你真需要这货?
- 第 13 章 数据流图
- 【记一笔】64位Win7和ubuntu13安装笔记
- Spring-13.模拟Filter实现
- Expression Blend实例中文教程(13) - 控件模板快速入门ControlTemplates
- /usr/local/mysql/bin/mysqld: Can't find file: './mysql/plugin.frm' (errno: 13 - Permission denied) 2
- HeadFirst 13 (包装器, 过滤器) not Finish