【问题描述】
 给定n个不同的整数,问这些数中有多少对整数,它们的值正好相差1。 
【输入形式】
  输入的第一行包含一个整数n,表示给定整数的个数。
  第二行包含所给定的n个整数。
【输出形式】
  输出一个整数,表示值正好相差1的数对的个数。
【样例输入】
 6
 10 2 6 3 7 8 
【样例输出】
     3 
【样例说明】
 值正好相差1的数对包括(2, 3), (6, 7), (7, 8)。
【评分标准】
  评测用例规模与约定
  1<=n<=1000,给定的整数为不超过10000的非负整数
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
   | #include <iostream> #include <vector>
  using namespace std;
  class Pair { public: 	int a; 	int b; 	bool operator==(const Pair& rhs) 	{ 		return (a == rhs.a) && (b == rhs.b); 	}	 };
  int main() { 	vector<int> num; 	int n, temp; 	int small, big;		 	vector<Pair> res;	 	Pair temp_;		     	cin >> n; 	for (int i = 0; i < n; ++i) 	{ 		cin >> temp; 		num.push_back(temp); 	}
  	for (auto i : num) 	{ 		small = i - 1; 		big = i + 1; 		if (count(num.begin(), num.end(), small))	 		{ 			temp_.a = small; 			temp_.b = i; 			if (!count(res.begin(), res.end(), temp_))	 				res.push_back(temp_); 		} 		if (count(num.begin(), num.end(), big))	 		{ 			temp_.a = i; 			temp_.b = big; 			if (!count(res.begin(), res.end(), temp_))	 				res.push_back(temp_); 		} 	}
  	cout << res.size(); 	return 0; }
 
   |