题解:P1914 小书童——凯撒密码
按题意模拟即可,读取位移量和原始字符串,再计算新字符的 ASCII 值,记得处理越界情况。
#include <iostream>
#include <string>
using namespace std;
int main() {
int n;
string s;
cin >> n >> s; // 读取位移量和原始字符串
for (char &c : s) {
// 计算新字符的ASCII值
int new_char = c + (n % 26); // 处理n>26的情况
// 处理超出z的情况
if (new_char > 'z') {
new_char -= 26;
}
c = new_char;
}
cout << s << endl;
return 0;
}