CCF 201409-1 相邻数对

试题编号: 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的非负整数。

关键词:set

 1 #include<iostream>
 2 #include<set>
 3 using namespace std;
 4 int main(){
 5     //freopen("in2.txt","r",stdin);
 6     set<int> s;
 7     int n;
 8     cin >> n;
 9     for(int i = 0;i<n;i++){
10         int buf;
11         cin >> buf;
12         s.insert(buf);
13     }
14     int bit = -2147483648;
15     int num = 0;
16     for(set<int>::iterator it = s.begin();it != s.end();it++){
17         if(*it == bit+1){
18             num++;
19         }
20         bit = *it;
21     }
22     cout << num;
23     return 0;
24 }

 

posted @ 2017-10-16 15:30  ywsswy  阅读(163)  评论(0编辑  收藏  举报