一般的手机的键盘是这样的:
要按出英文字母就必须要按数字键多下。例如要按出 x 就得按 9 两下,第一下会出 w,而第二下会把 w 变成 x。0 键按一下会出一个空格。
你的任务是读取若干句只包含英文小写字母和空格的句子,求出要在手机上打出这个句子至少需要按多少下键盘。
这道题直接暴力好吧(别想其他花里胡哨的做法)。
分为按1下、2下、3下、4下、按0这五种情况,定义5个计数器对每种情况分别计数就好。
注意别漏了空格(按0)的情况。
#include<bits/stdc++.h>
using namespace std;
int main()
{
int count1=0,count2=0,count3=0,count4=0,count5=0;
string a;
getline(cin,a);
for(int i=0;i<=a.size();i++)
{
if(a[i]=='a' || a[i]=='d' || a[i]=='g' || a[i]=='j' || a[i]=='m' || a[i]=='p' || a[i]=='t' || a[i]=='w')
count1++;
if(a[i]=='b' || a[i]=='e' || a[i]=='h' || a[i]=='k' || a[i]=='n' || a[i]=='q' || a[i]=='u' || a[i]=='x')
count2+=2;
if(a[i]=='c' || a[i]=='f' || a[i]=='i' || a[i]=='l' || a[i]=='o' || a[i]=='r' || a[i]=='v' || a[i]=='y')
count3+=3;
if(a[i]=='s' || a[i]=='z')
count4+=4;
if(a[i]==' ')
count5++;
}
cout<<count1+count2+count3+count4+count5<<endl;
return 0;
}