求助,为什么把+1删掉就能过?

P1914 小书童——凯撒密码

sxhy @ 2023-11-13 11:10:16

#include<iostream>
using namespace std;
#include<iomanip>
#include<algorithm>
#include<string>
#include<math.h>

int main() {

    std::ios::sync_with_stdio(false); std::cin.tie(0);
    string s;
    int n;
    cin>>n>>s;
    for(int i=0;i<s.length();i++)
   { if(s[i]+n>'z'){s[i]=((s[i]-'a'+1+n)%26-1+'a');cout << (char)s[i];}
    else{cout <<(char) (s[i]+n);}
}

}

把-‘a’+1和-1+‘a’的1删掉就可以过,不然就错第三个点


by HearTheWindSing @ 2023-11-13 11:51:23

@sxhy 比如当s[i]='a',n=25时有1就会出问题


by sxhy @ 2023-11-13 11:58:05

@HearTheWindSing 感谢


|