之前的代码,总有一处RE
#include<bits/stdc++.h>
using namespace std;
int n;
const int N=1e6+10;
int q[N],i,j,idx=0,mm,nn;
string st,temp;
int main(){
scanf("%d",&n);
getchar();
while(n--){
getline(cin,st);
for(int i=0,j=1;i<st.size();i++,j++){
while(st[j]!=' ' and j<st.size()){
j++;
}
while(i<j){
temp+=st[i++];
}
q[idx++]=stoi(temp);
temp.clear();
}
}
sort(&q[0],&q[idx-n]);
for(int i=0,j=1;j<idx-n and i<idx;i++,j++){
if(q[j]-q[i]==2){
mm=q[i]+1;
}
if(q[i]==q[j]){
nn=q[i];
}
}
printf("%d %d",mm,nn);
return 0;
}
chatGPT说stoi使用有问题,我重写了个stoi
int Mystoi(string st){
int temp=0;
for(int i=0;st[i]!='\0';i++){
temp=temp*10+st[i]-'0';
}
return temp;
}
终于把RE变为了WA 然后把getchar()换掉了 ,这是AC代码(蒟蒻非常直白的思路)
#include<bits/stdc++.h>
using namespace std;
int n;
const int N=1e6+10;
int q[N],i,j,idx=0,mm,nn;
string st,temp;
int Mystoi(string st){
int temp=0;
for(int i=0;st[i]!='\0';i++){
temp=temp*10+st[i]-'0';
}
return temp;
}
int main(){
scanf("%d",&n);
cin.ignore(INT_MAX,'\n');
while(n--){
getline(cin,st);
for(int i=0,j=1;i<st.size();i++,j++){
while(st[j]!=' ' and j<st.size()){
j++;
}
while(i<j){
temp+=st[i++];
}
if(temp.size()>=2)
q[idx++]=stoi(temp);
temp.clear();
}
}
sort(&q[0],&q[idx-n]);
for(int i=0,j=1;j<idx-n and i<idx;i++,j++){
if(q[j]-q[i]==2){
mm=q[i]+1;
}
if(q[i]==q[j]){
nn=q[i];
}
}
printf("%d %d",mm,nn);
return 0;
}