题目描述
按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。 如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下 如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。 现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。 现在给出一串字符,需要计算出它所需要花费的时间。
输入描述:
一个长度不大于100的字符串,其中只有手机按键上有的小写字母
输出描述:
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
示例1
输入
bob
www
输出
7
7
题目个人解析:手机九宫格键盘分布并不均匀,a~o均匀分布,每三个在一个键上。pqrs是4个字母在一个键上,tuv在一个键上,wxyz在最后一个键上。因为分布不均匀所以得根据条件进行计算。小写字母的键盘输入为ASCII码,a的真值为97。所以可以计算分布,再根据条件分别计算。
代码示例:
//建议第二种,直接暴力拆解
#include<iostream>
#include<stdio.h>
#include<cmath>
#include<algorithm>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
const int N=100;
//输入时间计算
int time(char c[N]){
int n=strlen(c); //计算字符串的长度
int temp1=0,temp2=0,temp3=0,t=0,index=0,count=0; //temp是分别计算分开部分的特征,t是总时间,index是ascii值
int numindex1=-1,numindex2=-1,numindex3=-1; //记录上一个字母的下标顺序
for(int i=0;i<n;i++){
index=int(c[i])-97; //计算此时输入字符的ascii的二进制值-97
//按键时间
if(index <= 14){
count = index%3+1;
}else{
if(15<=index && index <=18){
count = index-14;
}
if(18 < index && index<=21){
count = index-18;
}
if(22 <= index && index<=25){
count = index-21;
}
}
if(i == 0){ //当从0开始,只计算按键时间,不计算等待时间
t = count ;
if(index <= 14){
temp1 = index/3;
numindex1 = 0;
}else if(19 <= index && index<=21){
temp3 = (index-1)/3;
numindex3 = 0;
}else if((15<=index && index<=18) || (22 <= index && index<=25)){
temp2 = (index+1)/7;
numindex2 = 0;
}
}else{
t += count;
//等待时间
if(index <= 14){
if((i-numindex1)<=1){ //此时当前字母与上个字母相邻
if(temp1 == index/3){ // 计算是否是同个按键
t += 2;
}
} //同按键+2秒
temp1 = index/3;
numindex1 = i; //记录下标
}else if(19 <= index && index<=21){
if((i-numindex3)<=1){
if(temp3 == (index-1)/3){
t += 2;
}
} //同按键+2秒
temp3 = (index-1)/3;
numindex3 = i;
}
else if((15<=index && index<=18) || (22 <= index && index<=25)){
if((i-numindex2)<=1){
if(temp2 == (index+1)/7){
t += 2;
}
}
temp2 = (index+1)/7;
numindex2 = i;
}
}
}
return t;
}
int main(){
char str[N];
while(scanf("%s",str)!=EOF){
int t=time(str);
cout<<t<<endl;
}
return 0;
}
/*
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#include<fstream>
#include<iomanip>
#include<vector>
#include<regex>
#include<map>
#include<set>
#include<stack>
#include<queue>
using namespace std;
int main()
{
string str;
map<char , int> mp;
mp['a'] = 1;mp['b'] = 2;
mp['c'] = 3;mp['d'] = 1;
mp['e'] = 2;mp['f'] = 3;
mp['g'] = 1;mp['h'] = 2;
mp['i'] = 3;mp['j'] = 1;
mp['k'] = 2;mp['l'] = 3;
mp['m'] = 1;mp['n'] = 2;
mp['o'] = 3;mp['p'] = 1;
mp['q'] = 2;mp['r'] = 3;
mp['s'] = 4;mp['t'] = 1;
mp['u'] = 2;mp['v'] = 3;
mp['w'] = 1;mp['x'] = 2;
mp['y'] = 3;mp['z'] = 4;
while(cin >> str){
int rst = 0;
char f = NULL;
for(int i = 0 ; i < str.size() ; i++){
rst += mp[str[i]];
if(((f >= 'a' && (f <= 'c')) && (str[i] >= 'a' && str[i] <= 'c')) ||
((f >= 'd' && (f <= 'f')) && (str[i] >= 'd' && str[i] <= 'f')) ||
((f >= 'g' && (f <= 'i')) && (str[i] >= 'g' && str[i] <= 'i')) ||
((f >= 'j' && (f <= 'l')) && (str[i] >= 'j' && str[i] <= 'l')) ||
((f >= 'm' && (f <= 'o')) && (str[i] >= 'm' && str[i] <= 'o')) ||
((f >= 'p' && (f <= 's')) && (str[i] >= 'p' && str[i] <= 's')) ||
((f >= 't' && (f <= 'v')) && (str[i] >= 't' && str[i] <= 'v')) ||
((f >= 'w' && (f <= 'z')) && (str[i] >= 'w' && str[i] <= 'z'))){
rst += 2;
}
f = str[i];
}
cout << rst << endl;
}
return 0;
}
*/