题目:给你手机键盘,问你打出一句话需要按键次数,例如b是按2次,c是3次。
分析:模拟。打表记录每个按键次数下的字母,在用它反打表计算,每个字母的按键次数。
#include <iostream>
#include <cstdlib>
#include <cstdio>
using namespace std;
string str;
char mapc[5][10] = {""," adgjmptw","behknqux","cfilorvy","sz"};
int value[128];
int main()
{
for ( int i = 1 ; i < 5 ; ++ i )
for ( int j = 0 ; mapc[i][j] ; ++ j )
value[mapc[i][j]] = i;
int t;
while ( cin >> t ) {
getchar();
for ( int i = 1 ; i <= t ; ++ i ) {
getline( cin, str );
int sum = 0;
for ( int j = 0 ; str[j] ; ++ j )
sum += value[str[j]];
printf("Case #%d: %d\n",i,sum);
}
}
return 0;
}