求救大佬,为啥输入一大会乱码(第三四的WA)

P1914 小书童——凯撒密码

wangjialezuishuai @ 2020-03-16 16:29:42

#include<stdio.h>
int main()
{
    int n,i;
    char s[50];
    scanf("%d %s",&n,s);
    for(i=0;s[i]!='\0';i++)
    {
        s[i]=s[i]+n;
        if(s[i]>'z')  s[i]-=26;
        }
    printf("%s\n",s);
    return 0;

}

by Dimly_dust @ 2020-03-16 16:32:11

@wangjialezuishuai 没有啊w(゚Д゚)w


by Dimly_dust @ 2020-03-16 16:37:24

我的代码,你看看

#include<iostream>
using namespace std;
int main()
{
    int n,i;
    char s[50];
    scanf("%d %s",&n,s);
    n%=25;//特判
    for(i=0;s[i]!='\0';i++)
    {
        s[i]=s[i]+n;
        if(s[i]>'z')  s[i]-=26;
        cout<<s[i]; 
    }
    return 0;

}

by pidan @ 2020-03-16 16:38:23

if(s[i]>'z')那里错了,万一n>26怎么办


by wangjialezuishuai @ 2020-03-17 15:43:25

@Dimly_dust 为啥要n%=25;如果去掉是不是就不对了


by Dimly_dust @ 2020-03-17 16:12:40

@wangjialezuishuai 是的,要考虑大于26的情况


by wangjialezuishuai @ 2020-03-17 16:20:40

@Dimly_dust 好的,谢谢


by Dimly_dust @ 2020-03-17 16:22:40

@wangjialezuishuai 不用谢


|