#3#4 WA,是我的算法不好时间复杂度太大导致的吗但没有超时呀

P1914 小书童——凯撒密码

domelon @ 2022-09-05 07:23:33

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

by _Healer @ 2022-09-05 11:40:41

@domelon

#include<stdio.h>
#include<string.h>
int main()
{int n=0;
scanf("%d",&n);
char s[50]={0};
int k=0,a1=0,l=0;
scanf("%s",s);
for(a1=0;a1<sizeof(s);a1++){
    if(s[a1]!=0){
        if(s[a1]+n<='z'){
            l=s[a1]+n;
            printf("%c",l);
        }
        else{
            l=s[a1]+n-'z'+'a'-1;
            if(l<='z'){
                printf("%c",l);
            }
            else{//这里要用else括号括起来,否则当l<='z'时会输出两次(一次是if语句里,一次是执行里面的for之后)
                for(;l>'z';){
                k=l-'z';
                l='a'+k-1;
            }
            printf("%c",l);
            }
        }
    }
}
return 0;
}

by domelon @ 2022-11-09 19:15:44

谢谢谢谢


|