题目1079:手机键盘
上个手键盘图
我的思路是:把每个字母都映射到相应的按键上,在每个按键上根据顺序又有区分,m['a']=10,m['b']=11,m['c']=12,m['d']=20.......
这样hash表的值只要十位相同,就表示在一个按键上,个位上的数字加一就是要按得次数
#include<stdio.h>
#include<string.h>
int ans;
char s[120];
char m[256];
void init()
{
memset(m,0,sizeof(m));
m['a']=m['b']=m['c']=10;
m['b']+=1;
m['c']+=2;
m['d']=m['e']=m['f']=20;
m['e']+=1;
m['f']+=2;
m['g']=m['h']=m['i']=30;
m['h']+=1;
m['i']+=2;
m['j']=m['k']=m['l']=40;
m['k']+=1;
m['l']+=2;
m['m']=m['n']=m['o']=50;
m['n']+=1;
m['o']+=2;
m['p']=m['q']=m['r']=m['s']=60;
m['q']+=1;
m['r']+=2;
m['s']+=3;
m['t']=m['u']=m['v']=70;
m['u']+=1;
m['v']+=2;
m['w']=m['x']=m['y']=m['z']=80;
m['x']+=1;
m['y']+=2;
m['z']+=3;
}
int main(int argc, char *argv[])
{
// freopen("1079.in", "r", stdin);
init();
while(~scanf("%s",s))
{
ans=0;
for(int i=0;i<strlen(s);++i)
{
if(i==0){
ans+=(m[s[i]]%10+1);
}
else if(m[s[i]]/10==m[s[i-1]]/10){
ans+=2;
ans+=(m[s[i]]%10+1);
}
else
{
ans+=(m[s[i]]%10+1);
}
}
printf("%d\n",ans);
}
return 0;
}
/**************************************************************
Problem: 1079
User: kirchhoff
Language: C
Result: Accepted
Time:10 ms
Memory:912 kb
****************************************************************/