UVa10082
这道题如果用if
或者switch
来做代码就会变得冗长了,所以采用空间换时间的方法,先把错误字符到正确字符的映射关系记录下来。但是如果使用map
,那其实并没与达到效果,因为还需要硬编码映射关系。
书中提供的方法是把键盘上的字符从左到右先记录在string
中,然后查找错误的字符,直接取其左边的字符就好了。
#include <iostream>
#include <string>
using namespace std;
int main()
{
string str("`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./");
string strLine;
string::size_type pos;
while (getline(cin, strLine)){
for (auto c : strLine){
pos = str.find(c);
if (pos != string::npos){
cout << str[pos - 1];
}
else cout << c;
}
cout << endl;
}
return 0;
}