为啥错了

P1914 小书童——凯撒密码

benghuai @ 2021-02-12 10:25:41

#include <stdio.h>

int main()
{

    int i,c;
    char b[50] = {0};
    scanf("%d", &c);
    scanf("%s", b);
    for (i = 0; b[i] != '\0'; i++)
    {
        b[i] = b[i] +c;
        while (b[i] > 'z')
            b[i] = b[i] - 26;
    }
    printf("%s", b);
}

by asdfjkl @ 2021-02-12 10:39:49

@benghuai

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

        if (b[i]+n > 'z')
            b[i] = b[i] - 26;
        b[i]+=n;
    }
    printf("%s", b);
}

1.if(n>26) n%=26;

2.if (b[i]+n > 'z')

不谢


by benghuai @ 2021-02-12 10:41:42

@表情复杂 还是谢谢了。新年快乐


by asdfjkl @ 2021-02-12 10:44:37

@benghuai 新年快乐


|