日期:2026年2月11日
ZeroJudge 題目連結:c054. 10082 - WERTYU
解題想法
因為轉換的關係沒有編碼上的規律,用字典建表比較方便。
Python 程式碼
使用時間約為 7 ms,記憶體約為 2.9 MB,通過測試。
import sys
table = {'1': '`', '2': '1', '3': '2', '4': '3', '5': '4', '6': '5',
'7': '6', '8': '7', '9': '8', '0': '9', '-': '0', '=': '-',
'W': 'Q', 'E': 'W', 'R': 'E', 'T': 'R', 'Y': 'T', 'U': 'Y',
'I': 'U', 'O': 'I', 'P': 'O', '[': 'P', ']': '[', '\\': ']',
'S': 'A', 'D': 'S', 'F': 'D', 'G': 'F', 'H': 'G', 'J': 'H',
'K': 'J', 'L': 'K', ';': 'L', '\'': ';',
'X': 'Z', 'C': 'X', 'V': 'C', 'B': 'V', 'N': 'B',
'M': 'N', ',': 'M', '.': ',', '/': '.', ' ': ' '}
for line in sys.stdin:
s = line.rstrip()
t = []
for c in s: t.append(table.get(c, ' '))
print("".join(t))
C++ 程式碼
使用時間約為 1 ms,記憶體約為 316 kB,通過測試。
#include <iostream>
#include <string>
#include <map>
using namespace std;
int main() {
ios::sync_with_stdio(0); cin.tie(0);
map<char, char> table =
{{'1', '`'}, {'2', '1'}, {'3', '2'}, {'4', '3'}, {'5', '4'}, {'6', '5'},
{'7', '6'}, {'8', '7'}, {'9', '8'}, {'0', '9'}, {'-', '0'}, {'=', '-'},
{'W', 'Q'}, {'E', 'W'}, {'R', 'E'}, {'T', 'R'}, {'Y', 'T'}, {'U', 'Y'},
{'I', 'U'}, {'O', 'I'}, {'P', 'O'}, {'[', 'P'}, {']', '['}, {'\\', ']'},
{'S', 'A'}, {'D', 'S'}, {'F', 'D'}, {'G', 'F'}, {'H', 'G'}, {'J', 'H'},
{'K', 'J'}, {'L', 'K'}, {';', 'L'}, {'\'', ';'},
{'X', 'Z'}, {'C', 'X'}, {'V', 'C'}, {'B', 'V'}, {'N', 'B'},
{'M', 'N'}, {',', 'M'}, {'.', ','}, {'/', '.'}, {' ', ' '}};
string s, t;
while(getline(cin, s)) {
t.clear();
for(char c : s) t += table[c];
cout << t << "\n";
}
return 0;
}
沒有留言:
張貼留言