为啥用getline不行

P1055 [NOIP2008 普及组] ISBN 号码

Jak1r0 @ 2021-12-12 16:18:42

#include <bits/stdc++.h>

using namespace std;

int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    freopen("test.in", "r", stdin);
    freopen("test.out", "w", stdout);
    string str;
    while (cin>>str)
    {
        int sum = 0, start = 0;
        for (int i = 1; i <= 9; i++)
        {
            if (str[start] == '-')
                start++;
            sum += i * (str[start] - '0');
            start++;
        }
        sum %= 11;
        if (sum == 10)
            sum = 'X';
        else
            sum += '0';
        if (sum == str[str.length() - 1])
            cout << "Right" << endl;
        else
        {
            str[str.length() - 1] = sum;
            cout << str << endl;
        }
    }
}
为啥输入的时候用cin可以AC,用getline就不行?

by Titanfall @ 2021-12-12 16:20:05

getline会读入空格和换行 cin自动忽略空格和换行


by CarryQwQ @ 2021-12-12 16:20:54

视题目情况而定。

cin 遇到空格输入就结束了,

而 getline 可以输入空格。


by ssilrrr @ 2021-12-12 16:27:28

cin以空格和换行断句;getline以换行断句。比较方便的是cin流,避免getline。若将cin于getline在同一device上输入也会丢失数据


by Jak1r0 @ 2021-12-13 12:25:25

okkk


by 朴算子0101 @ 2022-01-20 20:36:21

真的服了,getline不是读取一行嘛cin只能处理没空格的字符串,,,,


|