题意
给你 T T T 组模拟密码串,输出每一串的明文,记得控制格式。
分析
纯模拟。
根据提供的表格判断即可。
简单倒是挺简单的,就是有点费手。
Code
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
int T;
string str, x, y;
char morse (string);
int main() {
// 输入 T 组摩斯密码
scanf ("%d", &T);
// 读取掉换行符
getchar();
for (int i = 1; i <= T; i++) {
// 控制格式
if (i != 1) printf ("\n");
// 输入摩斯密码字符串
getline (cin, str);
// 一个摩斯密码串,一个明文串
x = "", y = "";
for (int j = 0; j < str.size(); j++) {
// 存下摩斯密码
if (str[j] == '-' || str[j] == '.') x += str[j];
else {
// 转换明文
if (morse (x) != 'a') y += morse (x);
// 清空
x = "";
}
// 摩斯密码的连续两个空格就是明文的一个空格
if (str[j] == ' ' && str[j + 1] == ' ') y += ' ';
}
// 控制格式
printf ("Message #%d\n", i);
// 特判
if (morse (x) != 'a') y += morse (x);
// 输出明文串
cout << y << endl;
}
return 0;
}
char morse (string str) {
// 根据表格依次判断明文
if (str == ".-") return 'A';
else if (str == "-...") return 'B';
else if (str == "-.-.") return 'C';
else if (str == "-..") return 'D';
else if (str == ".") return 'E';
else if (str == "..-.") return 'F';
else if (str == "--.") return 'G';
else if (str == "....") return 'H';
else if (str == "..") return 'I';
else if (str == ".---") return 'J';
else if (str == "-.-") return 'K';
else if (str == ".-..") return 'L';
else if (str == "--") return 'M';
else if (str == "-.") return 'N';
else if (str == "---") return 'O';
else if (str == ".--.") return 'P';
else if (str == "--.-") return 'Q';
else if (str == ".-.") return 'R';
else if (str == "...") return 'S';
else if (str == "-") return 'T';
else if (str == "..-") return 'U';
else if (str == "...-") return 'V';
else if (str == ".--") return 'W';
else if (str == "-..-") return 'X';
else if (str == "-.--") return 'Y';
else if (str == "--..") return 'Z';
else if (str == "-----") return '0';
else if (str == ".----") return '1';
else if (str == "..---") return '2';
else if (str == "...--") return '3';
else if (str == "....-") return '4';
else if (str == ".....") return '5';
else if (str == "-....") return '6';
else if (str == "--...") return '7';
else if (str == "---..") return '8';
else if (str == "----.") return '9';
else if (str == ".-.-.-") return '.';
else if (str == "--..--") return ',';
else if (str == "..--..") return '?';
else if (str == ".----.") return '\'';
else if (str == "-.-.--") return '!';
else if (str == "-..-.") return '/';
else if (str == "-.--.") return '(';
else if (str == "-.--.-") return ')';
else if (str == ".-...") return '&';
else if (str == "---...") return ':';
else if (str == "-.-.-.") return ';';
else if (str == "-...-") return '=';
else if (str == ".-.-.") return '+';
else if (str == "-....-") return '-';
else if (str == "..--.-") return '_';
else if (str == ".-..-.") return '"';
else if (str == ".--.-.") return '@';
// 不是摩斯密码,返回一个固定的字母用于判断
else return 'a';
}
完结撒花。