蓝桥杯 错误票据

题目链接地址

  • http://lx.lanqiao.cn/problem.page?gpid=T28

注意

  • 我自己之前写的代码不需要ctrl+z结束输入,而蓝桥测评系统上应该是ctrl+z之后enter结束输入,因此无法结束,一直报错,在这里贴出自己的代码和别人的,当ctrl+z用于结束输入当前的while输入循环,enter表示从输入的缓冲区获取字符。

    #include
    #include 
    #include 
    #include 
    #include 
    #include 
    using namespace std;
    
    int main()
    {
        int n;
        while( (cin >> n) )
        {
            cout << n << endl;
        }
        return 0;
    }
    

    上面的代码中,每次输入后按下enter,会读入数据并输出,只有输入ctrl+z以及enter时,才会跳出输入的while循环。

  • 别人代码的网址:https://blog.csdn.net/milkcu/article/details/9090515

我的代码

#include
#include 
#include 
#include 
#include 
#include 
using namespace std;

int main()
{
    int lines;
    string str;
    cin >> lines;
    set result;
    int num1 = 0, num2 = 0;
    char s[100000];
    for (int i = 0; i < lines; i++)
    {
        memset( s, 0, sizeof(s)  );
        cin.clear();
        cin.sync();
        cin.getline(s, 100000);
        int index = 0;
        int num = 0;
        string str(s);
        //str.assign( s, s+strlen(s) );
        while( index < str.size() && str[index] == ' ' )
            index++; 
        while (index < str.size())
        {
            int currIdx = str.find(' ', index);
            if (currIdx != -1)
                num = atoi(str.substr(index, currIdx - index).c_str());
            else
                num = atoi(str.substr(index).c_str());

            if (result.count(num) == 1)
                num1 = num;
            result.insert(num);
            if (currIdx == -1)
                break;
            index = currIdx + 1;
            while (index < str.size() && str[index] == ' ')
                index++;
        }       
    }
    int start = *(result.begin());
    int diff = 0;
    for (set::iterator it = result.begin(); it != result.end(); it++)
    {
        if (*it - start != diff)
        {
            num2 = diff + start;
            break;
        }
        diff++;
    }
    cout << num2 << " " << num1 << endl;
    return 0;
}

网上别人的代码

#include 
#include 
#include 
#include 
using namespace std;
void print(int x) {
    cout << x << " ";
}
int main(void) {
    vector v;
    getchar();
    int tmp;
    //while(scanf("%d", &tmp) == 1) {
    while(cin >> tmp) {
        v.push_back(tmp);
    }
    sort(v.begin(), v.end());
    int m = 0;
    int n = 0;
    for(vector::iterator iv = v.begin(); iv != v.end(); iv++) {
        int tmp = *(iv + 1) - *iv;
        if(tmp == 2) {
            //duanhao
            m = *iv + 1;
        }
        if(tmp == 0) {
            //chonghao
            n = *iv;
        }
        if(m != 0 && n != 0) {
            break;
        }
    }
    cout << m << " " << n << endl;
    return 0;
}

你可能感兴趣的:(C++,C++primer)