60分,最后俩测试点错了,求助

P1914 小书童——凯撒密码

wangziqian1234wzq @ 2024-06-02 14:27:57

各位大佬帮忙看一下,谢谢了(跪)

#include<bits/stdc++.h>
using namespace std;
string a;
int n,l;
int main() {
    cin>>n;
    cin>>a;
    l=a.size();
    for(int i=0;i<l;i++){
        a[i]=char(a[i]+n);
        if(a[i]>'z'){
            a[i]=char(a[i]-26);
        }
    }
    cout<<a;
}

by Mathew_Miao @ 2024-06-02 14:42:07

#include<bits/stdc++.h>
using namespace std;
string a;
int n,l;
int main() {
    cin>>n;
    cin>>a;
    l=a.size();
    for(int i=0;i<l;i++){
        if(a[i]+n>'z'){
            a[i]=char(a[i]+n-26);
        }
        else{
            a[i]=char(a[i]+n);
        }
    }
    cout<<a;
}

by Mathew_Miao @ 2024-06-02 14:43:00

错误原因是 'z'+26 会爆 \operatorname{char}


by wangziqian1234wzq @ 2024-06-05 17:27:43

@Mathew_Miao 感谢大佬!!!


|