华为机考真题 -- 密码解密

题目描述:

给定一段"密文"字符串 s, 其中字符都是经过"密码本"映射的,现需要将"密文"解密并且输出映射的规则 ('a' - 'i')分别用('1' - '9')表示;('j' - 'z')分别用('10*' - '26*')表示约束:映射始终唯一

输入描述:

"密文"字符串

输出描述:

明文字符串

特别注意:

翻译后的文本的长度在 100 以内

示例1:

输入

20 * 19 * 20 *

输出

tst

C++源码:

cpp 复制代码
#include <iostream>
#include <string>

int main() {
	std::string s;
	if (!(std::cin >> s)) {
		return 0; // 如果输入失败,直接返回
	}

	std::string da = "";
	for (int i = 0; i < s.length(); ++i) {
		if (i + 2 < s.length() && s[i + 2] == '*') {
			char code = static_cast<char>(((s[i] - '1') * 10 + (s[i + 1] - '1')) + 'l' - 1);
			da += code;
			i += 2; // 跳过已经处理过的字符
		}
		else {
			char ch = static_cast<char>(s[i] - '1' + 'a');
			da += ch;
		}
	}
	std::cout << da << std::endl;

	system("pause");
	return 0;
}
相关推荐
ZPC821021 小时前
docker 镜像备份
人工智能·算法·fpga开发·机器人
ZPC821021 小时前
docker 使用GUI ROS2
人工智能·算法·fpga开发·机器人
琢磨先生David1 天前
Day1:基础入门·两数之和(LeetCode 1)
数据结构·算法·leetcode
哇哈哈20211 天前
信号量和信号
linux·c++
颜酱1 天前
栈的经典应用:从基础到进阶,解决LeetCode高频栈类问题
javascript·后端·算法
多恩Stone1 天前
【C++入门扫盲1】C++ 与 Python:类型、编译器/解释器与 CPU 的关系
开发语言·c++·人工智能·python·算法·3d·aigc
生信大杂烩1 天前
癌症中的“细胞邻域“:解码肿瘤微环境的空间密码 ——Nature Cancer 综述解读
人工智能·算法
王码码20351 天前
Flutter for OpenHarmony:socket_io_client 实时通信的事实标准(Node.js 后端的最佳拍档) 深度解析与鸿蒙适配指南
android·flutter·ui·华为·node.js·harmonyos
蜡笔小马1 天前
21.Boost.Geometry disjoint、distance、envelope、equals、expand和for_each算法接口详解
c++·算法·boost
m0_531237171 天前
C语言-数组练习进阶
c语言·开发语言·算法