题目来源:牛客网
牛牛的好朋友羊羊在纸上写了n+1个整数,羊羊接着抹除掉了一个整数,给牛牛猜他抹除掉的数字是什么。
牛牛知道羊羊写的整数神排序之后是一串连续的正整数,牛牛现在要猜出所有可能是抹除掉的整数。例如:
10 7 12 8 11 那么抹除掉的整数只可能是9
5 6 7 8 那么抹除掉的整数可能是4也可能是9
#include<iostream>
#include<string>
#include<map>
using namespace std;
int main()
{
int n;
cin >> n;
if (n <= 0)
{
cout << "mistake"; return 0;
}
map<int, int>num;
for (int i = 0; i < n; i++)
{
int j;
cin >> j;
//num[j]=1;
pair<int, int>newnum(j, j);
num.insert(newnum);
}
int prev = 0;
int lost = 0;
int min, max;
auto i = num.begin();
for (; i != num.end(); i++)
{
if (prev == 0)
{
prev = i->first;
min = i->first - 1;
}
else
{
if ((i->first - prev) == 2)
{
if (lost)
{
cout << "mistake";
return 0;
}
else
{
lost = prev + 1;
prev = i->first;
}
}
else if ((i->first - prev) > 1)
{
cout << "mistake";
return 0;
}
else
{
prev = i->first;
}
}
}
if (lost)
{
cout << lost;
}
else
{
max = prev + 1;
if (min>0)
cout << min << " " << max;
else
cout << max;
}
//cin.get();
//cin.get();
return 0;
}