蓝桥杯 PREV-5 错误票据

题目链接:

PREV-5 错误票据

思路:

等差数列求和再相减即可得到缺失的;
重复的用map记录下即可;

代码:

#include<bits/stdc++.h>

using namespace std;

typedef map<int, int>::iterator IT;

int main() {
#ifdef MyTest
	freopen("Sakura.txt", "r", stdin);
#endif
	map<int, int> mp;
	int x, mn = 1 << 30, mx = -1;
	scanf("%d", &x);
	while(~scanf("%d", &x)) ++mp[x], mn = min(mn, x), mx = max(mx, x);
	int a = (mn + mx) * (mx - mn + 1) / 2, b;
	for(IT it = mp.begin(); it != mp.end(); it++)  {
		if(it->second == 2) b = it->first;
		a -= it->first;
	}
	printf("%d %d", a, b);
	return 0;
}
发布了356 篇原创文章 · 获赞 12 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_45228537/article/details/104186574