题目描述
按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。 如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下 如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。 现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。 现在给出一串字符,需要计算出它所需要花费的时间。
输入描述:
一个长度不大于100的字符串,其中只有手机按键上有的小写字母
输出描述:
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
分析:
代码:
提交时出现过的错:
① 数组初始化结尾有一个分号;
② 每次 string 这个词都写错;
需要特别注意的:
int a1 =s[i]-'a';
int a2 =pre-'a';
if(number[a1]==number[a2])
time+=2;
time+=eachtime[a1];
代码:
#include
#include
using namespace std;
int main(){
string s;
int eachtime[]={1,2,3,
1,2,3,
1,2,3,
1,2,3,
1,2,3,
1,2,3,4,
1,2,3,
1,2,3,4};
int number[]={1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6,6,
7,7,7,
8,8,8,8};
while(cin>>s){
int len=s.length();
int time=0;
char pre='&';
for(int i=0;i