40分求助

P1914 小书童——凯撒密码

SNYQT_Howard @ 2022-09-05 11:37:05

#include<bits/stdc++.h>
#include<cstring> 
using namespace std;
char str[51];
int main(){
    int a ;
    cin >> a;
    cin >> str;
    for (int i = 0;;i ++){
        if (str[i] != '\0'){
            if (str[i] == 'z'){
                cout << "a";
            }
            else{
                 str[i] += a; 
                 cout << char(str[i]);  
            }
}
        else {
            break;
}
}
    return 0;
}

by 凤南天 @ 2022-09-05 12:29:18

@houword

if (str[i] == 'z'){
      cout << "a";
}

这是错的,显然。


by __Corrupt_X__ @ 2022-09-05 14:06:57

@houword 记住这句代码 str[i] = (str[i] - 'A' + a) % 26 + 'A'; 小写的话把'A'改为'a'


|