跳到正文

题解

题解:P1914 小书童——凯撒密码

#SUSPENDED#约 1 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月4日
篇幅
约 1 分钟
题目
P1914

版本

  1. v1当前

按题意模拟即可,读取位移量和原始字符串,再计算新字符的 ASCII 值,记得处理越界情况。

C++
#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;
}
原文