Set 题解

set 题解

题目:
Doubles poj 1552
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 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
题意:
给出几个数,计算每个数的两倍在原序列中出现的次数。
用set存数,遍历一遍set用count判断该数的两倍有没有出现。
代码:

#include <iostream>
#include <set>
using namespace std;
int main()
{
	int a;
	while (cin >> a, a != -1) {
		set<int>s;
		while (a)
			s.insert(a), cin >> a;
		int cnt = 0;
		for (set<int>::iterator i = s.begin(); i != s.end(); i++)
			if (s.count(*i * 2))cnt++;
		cout << cnt << endl;
	}
}

题目:
{A} + {B} hdu 1412
Problem Description
给你两个集合,要求{A} + {B}.
注:同一个集合中不会有两个相同的元素.

Input
每组输入数据分为三行,第一行有两个数字n,m(0<n,m<=10000),分别表示集合A和集合B的元素个数.后两行分别表示集合A和集合B.每个元素为不超出int范围的整数,每个元素之间有一个空格隔开.

Output
针对每组数据输出一行数据,表示合并后的集合,要求从小到大输出,每个元素之间有一个空格隔开.

Sample Input
1 2
1
2 3
1 2
1
1 2

Sample Output
1 2 3
1 2

题意:
给出两个序列 输出合并后的序列,相当于用set去重。
代码:

#include <iostream>
#include <set>
using namespace std;
int main()
{
	int n, m, a;
	while (cin >> n >> m)
	{
		n += m;
		set<int>s;
		while (n--)
			cin >> a, s.insert(a);
		for (auto i = s.begin(); i != s.end(); i++,cout << (i==s.end()?"\n":" "))
			cout << *i;
	}
}

题目:
单词数 hdu 2072

Problem Description
lily的好朋友xiaoou333最近很空,他想了一件没有什么意义的事情,就是统计一篇文章里不同单词的总数。下面你的任务是帮助xiaoou333解决这个问题。

Input
有多组数据,每组一行,每组就是一篇小文章。每篇小文章都是由小写字母和空格组成,没有标点符号,遇到#时表示输入结束。

Output
每组只输出一个整数,其单独成行,该整数代表一篇文章里不同单词的总数。

Sample Input
you are my friend
#

Sample Output
4
题意:
给出一行字符串,单词用一个或多个空格隔开,求单词的种类有多少。用set存string自动去重,输出大小即可。判断空格找单词比较麻烦,学一下stringstream就很方便了。
代码:
不使用stringstream:

#include<iostream>
#include<string>
#include<set>
using namespace std;
string t, x;
set<string>s;
int main() {
	while (getline(cin, t)) {
		s.clear();
		x = "";
		if (t == "#") 
			break;
	//不使用stringstream的话判断出一个单词会比较麻烦 容易wa
		int n = t.length();
		for (int i = 0; i < n; ++i) 
			if (isalpha(t[i]))
				x += t[i];
			else if (x.size())
				s.insert(x), x = "";
		if (x.size())s.insert(x);
		cout << s.size() << endl;
	}
	return 0;
}

使用stringstream:

#include<iostream>
#include<string>
#include<sstream>
#include<set>
using namespace std;
string t, x;
set<string>s;
int main() {
	while (getline(cin, t),t!="#") {
		s.clear();
		stringstream mid;
		mid << t;
		while (mid >> x)
			s.insert(x);
		cout << s.size() << endl;
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值