题目描述
某涉密单位下发了某种票据,并要在年终全部收回。
每张票据有唯一的ID号。
全年所有票据的ID号是连续的,但ID的开始数码是随机选定的。
因为工作人员疏忽,在录入ID号的时候发生了一处错误,造成了某个ID断号,另外一个ID重号。
你的任务是通过编程,找出断号的ID和重号的ID。
假设断号不可能发生在最大和最小号。
样例
输入样例:
2
5 6 8 11 9
10 12 9
输出样例:
7 9
算法1(模拟)
C++ 代码
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 100010;
int n;
int a[N];//记录数字出现的次数
int main()
{
cin >> n;
while(n--)
{
int num;
while(cin >> num) a[num]++;
}
int start;
for(int i = 1; i <= 100000; i++)//找到连号的起始位置
if(a[i])
{
start = i;
break;
}
int end;
for(int i = 100000; i >= 1; i--)//找到连号的终止位置
if(a[i])
{
end = i;
break;
}
int p, q;
for(int i = start; i <= end; i++)//遍历连号区间 找到断号和重号
{
if(a[i] == 0) p = i;
if(a[i] == 2) q = i;
}
cout << p << ' ' << q << endl;
return 0;
}
算法2(排序)
C++ 代码
#include <cstring>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;
const int N = 10010;
int n;
int a[N];
int main()
{
int cnt;
cin >> cnt;
string line;
getline(cin, line); // 忽略掉第一行的回车
while (cnt -- )
{
getline(cin, line);
stringstream ssin(line);
while (ssin >> a[n]) n ++ ;
}
sort(a, a + n);
int res1, res2;
for (int i = 1; i < n; i ++ )
if (a[i] == a[i - 1]) res2 = a[i]; // 重号
else if (a[i] >= a[i - 1] + 2) res1 = a[i] - 1; // 断号
cout << res1 << ' ' << res2 << endl;
return 0;
}