题意:
有n个人,每个人有一个值那是缆车的值
比如 1 2 2 3 7 5
就是有5个缆车,6个人
5个缆车的编号为1 2 3 5 7
2 3缆车可以满足条件,在2 3缆车上有3个人,所以亲3下
解析:
先排序,添加首尾,并模拟计算。
注意:
缆车的值要用long long保存,不然会爆掉WA。
AC代码
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <map>
using namespace std;
typedef long long ll;
const int INF = 2147483647;
const int N = 105;
map<ll,int> hash;
ll a[N];
int main() {
int cas = 1, n, val;
while(scanf("%d", &n) != EOF) {
hash.clear();
for(int i = 1; i <= n; i++) {
scanf("%d" ,&val);
hash[val]++;
}
if(hash.size() == 1) {
printf("Case #%d: -1\n",cas++);
continue;
}
map<ll,int>::iterator it;
int tot = 1 , cnt = 0;
for(it = hash.begin(); it != hash.end(); it++) {
a[tot++] = it->first;
}
tot--;
a[0] = a[tot] , a[tot+1] = a[1];
for(int i = 1; i <= tot; i++) {
if((a[i] + a[i-1]) % INF == a[i+1]) {
cnt += hash[a[i]];
}
}
printf("Case #%d: %d\n", cas++, cnt);
}
return 0;
}