C语言,请大佬看看这个为什么全部WA,自己测试的时候感觉没问题呀

P1914 小书童——凯撒密码

wzsl66 @ 2020-02-16 18:50:19


#include<stdio.h>
#include<string.h>
int main()
{
    char str[51];
    int i,j,n,length;
    scanf("%d",&n);
    n=n%26;
    getchar();
    gets(str);
    length=strlen(str);
    for(i=0;i<length;i++)
    {
        if(str[i]+n<='z') str[i]=str[i]+n;
        else str[i]=str[i]+n-26;
    }
    printf("%s",str);
    return 0;
}

by 翰森锐 @ 2020-02-16 18:51:09

用getline不gets


by zhanghengrui @ 2020-02-16 19:10:05

@翰森锐 C 哪来的 getline


by zhaoyp @ 2020-02-16 19:40:16

gets有风险


by zhaoyp @ 2020-02-16 19:41:39

稍微改动一下(C++)

#include<bits/stdc++.h>
using namespace std;
int main()
{
    string str;
    int i,j,n,length;
    scanf("%d",&n);
    n=n%26;
    cin >> str;
    length=str.size();
    for(i=0;i<length;i++)
    {
        if(str[i]+n<='z') str[i]=str[i]+n;
        else str[i]=str[i]+n-26;
    }
    cout << str;
    return 0;
}

by wzsl66 @ 2020-02-16 20:14:24

十分感谢


by ZRYSSSBBB @ 2020-02-18 16:02:32

我做题时思路基本和你一样的,用个getchar吸收回车符号然后直接用gets获取字符串,但是死活不给过,人也是醉了。。


|