帮帮忙!!

P1914 小书童——凯撒密码

Zcus @ 2017-04-19 20:48:14

  #include<stdio.h>
#include<string.h> 
int n,i,l;
char s[256];
int main(){

    scanf("%d",&n);
    scanf("%s",s);
    l=strlen(s);
    for(i=0;i<l;i++){
        s[i]+=n;
        if(s[i]>'z'){
            s[i]=s[i]-'z'+'a'-1;
        }
    }
    printf("%s",s);
    return 0;
}

by 双管荧光灯 @ 2017-04-19 21:53:46

你的s[i]加上n爆出char的上限了


by 双管荧光灯 @ 2017-04-19 21:55:43

#include<stdio.h>
#include<string.h> 
int n,i,l;
char s[256];
int main(){
    scanf("%d\n",&n);
    scanf("%s",s);
    l=strlen(s);
    for(i=0;i<l;i++){
        if(n+s[i]>'z'){
            s[i]=s[i]-'z'+'a'-1+n;
        }
        else
            s[i]+=n;
    }
    printf("%s",s);
    return 0;
}

|