getline(cin, s)遇回车结束读入
vector + 哈希,很顽强的a了😂
#include <bits/stdc++.h>
using namespace std;
vector<int> alls;
unordered_map<int, int> mp;
const int N = 1e5 + 10;
string s;
int n;
int main()
{
cin >> n;
getchar();
for(int i = 1; i <= n; i ++ )
{
getline(cin, s);
// cout << s.size() << " js" << endl;
for(int i = 0; i < s.size(); i ++ )
{
if(isdigit(s[i]))
{
int j = i, x = 0;
while(isdigit(s[j]))
{
x = x * 10 + (s[j] - '0');
j ++;
}
alls.push_back(x);
i = j - 1;
}
}
s.clear();
}
sort(alls.begin(), alls.end());
int start = 0, end = 0;
for(auto it : alls)
{
start = it;
break;
}
// cout << start << "dsc" << endl;
reverse(alls.begin(), alls.end());
for(auto it : alls)
{
end = it;
break;
}
// cout << end << " dsjk " << endl;
int dh = 0, ch = 0;
for(auto it : alls)
mp[it] ++;
for(int i = start; i <= end; i ++ )
{
if(mp[i] == 0) dh = i;
if(mp[i] > 1) ch = i;
}
cout << dh << ' ' << ch << endl;
return 0;
}
还是写数组简单点,不过没想到断号相差2及以上的特点
#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
string s;
int a[N];
int n, cnt;
int main()
{
cin >> n;
getchar();
for(int i = 1; i <= n; i ++ )
{
getline(cin, s);
// cout << s.size() << " js" << endl;
for(int i = 0; i < s.size(); i ++ )
{
if(isdigit(s[i]))
{
int j = i, x = 0;
while(isdigit(s[j]))
{
x = x * 10 + (s[j] - '0');
j ++;
}
a[cnt ++] = x;
i = j - 1;
}
}
s.clear();
}
sort(a, a + cnt);
int dh = 0, ch = 0; //断号dh,重号ch
for(int i = 0; i < cnt - 1; i ++ )
{
if(a[i] == a[i + 1]) ch = a[i];
if(a[i] + 2 <= a[i + 1]) dh = a[i] + 1;
}
// for(int i = 0; i < cnt; i ++ ) cout << a[i] << ' ';
cout << dh << ' ' << ch << endl;
return 0;
}