到底要不要吞回车?求大佬解答

P1914 小书童——凯撒密码

smallpeople @ 2023-12-06 18:02:04

AC代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s;

    cin>>n;

    cin>>s;

    for(int i = 0;i < s.size();i ++)
    {
        int ss = s[i] + n;
        if(ss > 'z')ss -= 26;
        s[i] = ss;
    }

    cout<<s;
    return 0;
}

全WA代码:

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s;

    cin>>n;
    getchar();//吞回车
    getline(cin,s); 

    for(int i = 0;i < s.size();i ++)
    {
        int ss = s[i] + n;
        if(ss > 'z')ss -= 26;
        s[i] = ss;
    }

    cout<<s;
    return 0;
}

为啥第二个不行啊?如果用getline输入不是应该吞回车吗???


by _____QWQ_____ @ 2023-12-06 18:06:36

不用啊,cin可以以回车结束的


by _____QWQ_____ @ 2023-12-06 18:07:13

输入n之后就自动进入字符串s的输入了


by sdyzpf @ 2023-12-06 18:26:18

@smallpeople

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s;

    cin>>n;
    char c=getchar();
    c=getchar();
    getline(cin,s);
    for(int i = 0;i < s.size();i ++)
    {
        int ss = s[i] + n;
        if(ss > 'z')ss -= 26;
        s[i] = ss;
    }

    cout<<s;
    return 0;
}

by sdyzpf @ 2023-12-06 18:28:10

@smallpeople 这题数据好像有点问题


by sdyzpf @ 2023-12-06 18:28:35

@smallpeople 要吞两次回车


by sdyzpf @ 2023-12-06 18:31:22

@smallpeople 代码有个问题,n如果比较大的话,ss可能要减不止一次26

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int n;
    string s;

    cin>>n;
    char c=getchar();
    c=getchar();
    getline(cin,s);
    for(int i = 0;i < s.size();i ++)
    {
        int ss = s[i] + n;
        while(ss > 'z')ss -= 26;
        s[i] = ss;
    }

    cout<<s;
    return 0;
}

by sdyzpf @ 2023-12-06 18:52:24

@smallpeople 正常题目只要吞一次换行就够了,是这题数据的问题,我跟管理反馈下


by xinxin88_0v0 @ 2023-12-06 19:15:58

吞回车可以用Ctrl+Z解


by smallpeople @ 2023-12-07 18:51:37

@sdyzpf 谢谢,懂啦!


|