1204. 错误票据(经典的输入处理方式)

题目链接:

https://www.acwing.com/problem/content/1206/

题解:

(1)getline()不会读 ,并且会抛弃读到的 ,而且不会留在缓冲区中。

(2)cin不会读 ,但是会把它留在缓冲区中等其他人读入

(3)stringstream在字符串、数字互转时很有用

AC代码:

#include<iostream>
#include <sstream>
#include <algorithm>
#include <string>

using namespace std;

const int N = 10000+5;
int a[N];
int n;

int main(){
    int cnt;
    cin >> cnt;
    string line;
    
    getline(cin,line);
    while(cnt--){
        getline(cin,line);
        stringstream ss(line);
        while(ss >> a[n]) n++;
    }
    
    
    stable_sort(a,a+n);
    int res1,res2;
    for(int i=1;i<n;i++){
        if(a[i] == a[i-1]) res2 = a[i];
        if(a[i] == a[i-1] + 2) res1 = a[i]-1;
    }
    
    cout << res1 << " " << res2 << endl;
    return 0;
}
原文地址:https://www.cnblogs.com/doubest/p/12297088.html