只有40分求助!

P1914 小书童——凯撒密码

LSR120218_juruo @ 2024-03-02 22:03:35

#include <iostream>
using namespace std;

int main() {
    int n,ans=0;
    char a[53];
    cin >>n;
    cin>>a;
    while(a[ans]!='\0'){
        if(a[ans]=='z'){
            a[ans]='a'+n-1;
        }else{
            a[ans]+=n;
        }
        ans++;
    }
    cout<<a;

    return 0;
}

那里出问题了(3,4,5没过)?


by wfls_zhuyiwen @ 2024-03-02 22:17:21

这里

if(a[ans]=='z'){
    a[ans]='a'+n-1;

应该是

if(a[ans]=='z'- n){
    a[ans]='a'+n-1;

by wfls_zhuyiwen @ 2024-03-02 22:20:27

@LSR120218_juruo AC code:

#include <iostream>
using namespace std;

int main() {
    int n,ans=0;
    char a[53];
    cin >>n;
    cin>>a;
    while(a[ans]!='\0'){
        if(a[ans]+n>'z'){
            a[ans]= (a[ans] + n) - 26;
        }else{
            a[ans]+=n;
        }
        ans++;
    }
    cout<<a;

    return 0;
}

by szx6688 @ 2024-03-03 09:01:16

我不知道(笑脸)


by LSR120218_juruo @ 2024-03-03 13:35:44

@szx6688 呃........


by LSR120218_juruo @ 2024-03-03 13:44:04

@zhuyiwen10 thank you


|