DreamJudge-1014-加密算法

1.题目介绍

Time Limit: 1000 ms
Memory Limit: 256 mb

编写加密程序,加密规则为:将所有字母转化为该字母后的第三个字母,即A->D、B->E、C->F、......、Y->B、Z->C。小写字母同上,其他字符不做转化。输入任意字符串,输出加密后的结果。
例如:输入"I love 007",输出"L oryh 007"

输入输出格式

输入描述:

输入一行字符串,长度小于100。

输出描述:

输出加密之后的结果。

输入输出样例

输入样例#:

I love 007

输出样例#:

L oryh 007

题目来源
华南师范大学/贵州大学机试题

2.题解

2.1 自动打表 + 字符串转换

思路

手动打表太累?直接自动打表
同时注意下cin读到空格停止,所以这里使用getline进行读取!!!

代码

#include<bits/stdc++.h>
using namespace std;
//vector<char> lowerLetters{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
vector<char> lowerLetters(26), upperLetters(26);
int main(){
	for(char ch = 'a'; ch <= 'z'; ch++){
		lowerLetters[ch - 'a'] = ch;
		upperLetters[ch - 'a'] = ch - 32;
	}
	string str;
	getline(cin, str);
	for(char &ch: str){
		if(ch >= 'a' && ch <= 'z'){
			ch = lowerLetters[(ch - 'a' + 3) % 26];
		} else if(ch >= 'A' && ch <= 'Z'){
			ch = upperLetters[(ch - 'A' + 3) % 26];
		}
	}
	cout << str;
	return 0;
}
posted @   DawnTraveler  阅读(7)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· DeepSeek 开源周回顾「GitHub 热点速览」
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
点击右上角即可分享
微信分享提示