题解 P1765 【手机_NOI导刊2010普及(10)】
Randyhoads
2017-11-05 10:51:45
这道题其实可以用map来做,map就是映射,在这里可以把一个字符映射成一个数,就是要按几次才能按出这个数
其实map的功能很强大,这里只用了他的基本性质
代码~
```cpp
#include<cstdio>
#include<map>
#include<cstring>
#include<iostream>
#include<string>
using namespace std;
map<char,int>ma;//定义map,把char类型转化成int
string word;
int ans=0;
void init()
{
ma['a']=1;
ma['b']=2;
ma['c']=3;
ma['d']=1;
ma['e']=2;
ma['f']=3;
ma['g']=1;
ma['h']=2;
ma['i']=3;
ma['j']=1;
ma['k']=2;
ma['l']=3;
ma['m']=1;
ma['n']=2;
ma['o']=3;
ma['p']=1;
ma['q']=2;
ma['r']=3;
ma['s']=4;
ma['t']=1;
ma['u']=2;
ma['v']=3;
ma['w']=1;
ma['x']=2;
ma['y']=3;
ma['z']=4;
ma[' ']=1;
```
}//初始化
```cpp
int main()
{
init();
getline(cin,word);//读入
for(int i=0;i<word.length();i++)
{
ans+=ma[word[i]];
}
cout<<ans<<endl;
}
```