为什么不给过,救救孩子吧

P1914 小书童——凯撒密码

ykily @ 2021-03-07 20:49:31

#include<iostream>
#include<cstdio>

using namespace std;

int main()
{
    int b,i=-1,k=0;
     scanf("%d\n",&b);
    char a[100];
    do
     {
        ++i;
        scanf("%c",&a[i]);
     }
     while(a[i]!='\n');
    while(k!=i)
    {
        a[k]+=b;
        if(a[k]>'z')
        a[k]=a[k]-'z'+'a'-1;     
        cout<<a[k];
         ++k;
    }
    return 0;
}

by dingshengyang @ 2021-03-07 20:52:10

因为不给过


by _Sparkle @ 2021-03-07 20:59:42

@ykily b可能大于26


by Maxmilite @ 2021-03-07 21:11:06

#include<iostream>
#include<cstdio>
    // Added a line here.
#include <cstring>

using namespace std;

int main()
{
    int b,i=-1,k=0;
     scanf("%d",&b);
    char a[105];
    // Added a line here.
    memset(a, 0, sizeof(a));

    // Deleted a function here.

    /* 
    do
     {
        ++i;
        scanf("%c",&a[i]);
     }
     while(a[i]!='\n');
    */

    // Added two lines here.

    scanf ("%s", a);
    i = strlen(a);

    while(k!=i)
    {
        // swapped the three lines.
        if(a[k] + b>'z')
        a[k]=a[k]-'z'+'a'-1;
        a[k]+=b;
        cout<<a[k];
         ++k;
    }
    return 0;
}

顺便帮着改了点其他不规范的地方

如果 k 是 26,char'z' 的话,就爆掉了


by Maxmilite @ 2021-03-07 21:11:13

@ykily


|