历届习题 错误票据 (读入技巧,运行超时)

来源:互联网 发布:python vps 编辑:程序博客网 时间:2024/06/09 14:39

刚开始看到题目的时候用了getchar,,,然后就陷入了超时中。因为其中不知道有多少个空格,如果是getchar ,要读很久。。
然后还有一个是用scanf读入数据,然后getchar读入后面一个字符,判断是不是\n,但是还是有问题啊,谁知道一行的数字最后有没有空格呢~
然后我用的stringsstream

#include <iostream>#include <cstring>#include <algorithm>#include <cstdio>#include <cmath>#include <sstream>using namespace std;int vis[100010];int main(){    int n,minn=100000,maxx=0,re=0,lack=0;    scanf("%d",&n);    getchar();    for(int i=0;i<n;i++)    {        string s;        getline(cin,s);        int x;        stringstream ss(s);        while(ss>>x) {            if(x>maxx) maxx=x;            if(x<minn) minn=x;            if(vis[x]) re=x;            vis[x]=1;        }    }    for(int i=minn+1;i<maxx;i++)    {        if(lack) break;        else if(vis[i]==0) lack=i;    }    printf("%d %d\n",lack,re);    return 0;}
0 0
原创粉丝点击