密码翻译

GitHub跳转原题关系图返回列表

逐字符将英文字母后移一位,并分别处理小写与大写字母的循环边界。

OJ: noi_openjudge

题目 ID: ch0107-09

难度:入门

标签:字符串模拟python

日期: 2026-07-30 23:01

题意

将字母替换为字母表后继,z 循环到 aZ 循环到 A,其他字符不变。

思路

普通字母可用 ASCII 码加一;zZ 是循环边界,要单独映射。生成器逐字符调用 encrypt 后拼接输出。

代码

Python代码

python
def encrypt(character: str) -> str:
    if "a" <= character <= "y" or "A" <= character <= "Y":
        return chr(ord(character) + 1)
    if character == "z":
        return "a"
    if character == "Z":
        return "A"
    return character


print("".join(encrypt(character) for character in input()))

C++代码

cpp
#include <cstdio>
#include <cstring>
#define maxn 500

int n;
char str[maxn];
int idx = 1;

char _map[maxn];


int main(){
    char t;
    int i;
    for (i='a';i<='z';i++){
        _map[i] = i+1;
    }
    _map['z'] = 'a';
    for (i='A';i<='Z';i++){
        _map[i] = i+1;
    }
    _map['Z'] = 'A';
    while(1){
        int ans = scanf("%c",&t);
        if( t== '\n' || t == '\r' || ans == EOF)
            break;
        str[idx] = t;
        idx++;
    }
    idx--;
    for (i=1;i<=idx;i++){
        char c = str[i];
        if( ( c >='a' && c <='z') ||( c >='A' && c <='Z') )
            printf("%c",_map[c]);
        else
            printf("%c",c);
    }
    return 0;
}

复杂度

时间复杂度和输出空间均为 O(n)O(n)

总结

字符平移题最重要的边界是字母表末尾的回绕。