CCF CSP 编程题目和解答-----试题名称:相邻数对-------201409-1
2017-03-17 00:37
471 查看
问题描述
试题编号: | 201409-1 |
试题名称: | 相邻数对 |
时间限制: | 1.0s |
内存限制: | 256.0MB |
问题描述: | 问题描述 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1。 输入格式 输入的第一行包含一个整数n,表示给定整数的个数。 第二行包含所给定的n个整数。 输出格式 输出一个整数,表示值正好相差1的数对的个数。 样例输入 6 10 2 6 3 7 8 样例输出 3 样例说明 值正好相差1的数对包括(2, 3), (6, 7), (7, 8)。 评测用例规模与约定 1<=n<=1000,给定的整数为不超过10000的非负整数。 |
#include<iostream> #include<stdlib.h> #include<vector> #include<algorithm> #include<string> #include<map> #include<strstream> using namespace std; int main() { int n; cin >> n ; map<int, int> all; for (int i = 0; i < n; i++) { int tem; cin >> tem; all.insert(pair<int,int>(tem,0)); } int count = 0; map<int, int>::iterator it = all.begin(); for (it; it != all.end(); it++) { int tem = it->first; int less = tem - 1; int more = tem + 1; if (all.find(less) != all.end()) count++; if (all.find(more) != all.end()) count++; } count /= 2;//所有的对子都重复算了两遍 cout << count; }
相关文章推荐
- CCF CSP 编程题目和解答-----试题名称:画图-------201409-2
- CCF CSP 编程题目和解答-----试题名称: 最大的矩形 -------201312-3
- CCF CSP 编程题目和解答-----试题名称:火车购票-------201609-2
- CCF CSP 编程题目和解答-----试题名称:权限查询-------201612-3
- CCF CSP 编程题目和解答-----试题名称:路径解析 -------201604-3
- CCF CSP 编程题目和解答-----试题名称:Z字形扫描-------201412-2
- CCF CSP 编程题目和解答-----试题名称: 出现次数最多的数 --------201312-1
- CCF CSP 编程题目和解答-----试题名称:消除类游戏-------201512-2
- CCF CSP 编程题目和解答-----试题名称:日期计算-------201509-2
- CCF CSP 编程题目和解答-----试题名称:炉石传说 -------201609-3
- CCF CSP 编程题目和解答-----试题名称:模板生成系统 -------201509-3
- CCF CSP 编程题目和解答-----试题名称:俄罗斯方块-------201604-2
- CCF CSP 编程题目和解答-----试题名称:窗口-------201403-2
- CCF CSP 编程题目和解答-----试题名称:工资计算-------201612-2
- CCF CSP 编程题目和解答-----试题名称: ISBN 号码 -------201312-2
- CCF CSP 编程题目和解答-----试题名称:画图 -------201512-3
- CCFCSP 201409-1相邻数对
- CCF CSP 201409-1 相邻数对
- CCF CSP试题 201409-2 画图
- CCF CSP 201409-1 相邻数对