AcWing 1204. 错误票据
原题链接
简单
作者:
Bug-Free
,
2020-09-21 23:25:21
,
所有人可见
,
阅读 671
算法一: 排序+判断
//
// Created by Genes on 2020/9/15.
//
// 错误票据
// 算法一: 排序+判断
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;
const int N = 1e4 + 10;
int a[N];
int cnt;
int main() {
ios::sync_with_stdio(false);
string line;
cin >> cnt;
getline(cin, line); //忽略第一行空格
int k = 0;
while (cnt--) {
getline(cin, line);
stringstream ssin(line);
while (ssin >> a[k]) k++;
}
sort(a, a + k);
int n = 0, m = 0;
for (int i = 1; i < k; i++) {
if (a[i] == a[i - 1]) {
n = a[i];
} else if (a[i] >= a[i - 1] + 2) {
m = a[i] - 1;
}
}
cout << m << " " << n << endl;
return 0;
}
算法二: bool数组+判断
//
// Created by Genes on 2020/9/21.
//
// 错误票据
// 算法二: 开bool数组
#include<iostream>
#include<sstream>
#include<climits>
using namespace std;
const int N = 1e5 + 10;
int a[N];
int cnt;
bool st[N];
int main() {
ios::sync_with_stdio(false);
string line;
cin >> cnt;
getline(cin, line); //去除第一行的换行
int k = 0;
int minN = INT_MAX, maxN = INT_MIN;
while (cnt--) {
getline(cin, line);
stringstream ssin(line);
while (ssin >> a[k]) {
if (minN > a[k]) minN = a[k];
if (maxN < a[k]) maxN = a[k];
k++;
}
}
int n = 0, m = 0;
for (int i = 0; i < k; i++) {
if (!st[a[i]]) {
st[a[i]] = true;
} else {
n = a[i];
}
}
for (int i = minN + 1; i < maxN; i++) {
if (!st[i]) {
m = i;
break;
}
}
cout << m << " " << n << endl;
return 0;
}