时间限制:1 秒内存限制:32 兆特殊判题:否提交:2746解决:1521
题目描述:
按照手机键盘输入字母的方式,计算所花费的时间
如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。
如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下
如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。
现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。
现在给出一串字符,需要计算出它所需要花费的时间。
输入:
一个长度不大于100的字符串,其中只有手机按键上有的小写字母
输出:
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
样例输入:
bob
www
样例输出:
7
7
解题报告:在做该题目时,要注意两点:1、判断两个连续字母是否位于同一个键上;2、字符串中只包括一个字母的情况,程序写的不是很简洁,有待完善
源代码:
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
#include
<iostream>#include
<string.h>#include
<cstring>#include
<map> using
namespace
std; map<int,char>
m;int
char2num(char
ch){ int
num;if(ch=='a'||ch=='d'||ch=='g'||ch=='j'||ch=='m'||ch=='p'||ch=='t'||ch=='w')num=1;else
if(ch=='b'||ch=='e'||ch=='h'||ch=='k'||ch=='n'||ch=='q'||ch=='u'||ch=='x')num=2;else
if(ch=='c'||ch=='f'||ch=='i'||ch=='l'||ch=='o'||ch=='r'||ch=='v'||ch=='y')num=3;else
if(ch=='s'||ch=='z')num=4;return
num;}int
charmap(char
ch){if(ch=='a'||ch=='b'||ch=='c')m[ch]=1;else
if(ch=='d'||ch=='e'||ch=='f')m[ch]=2;else
if(ch=='g'||ch=='h'||ch=='i')m[ch]=3;else
if(ch=='j'||ch=='k'||ch=='l')m[ch]=4;else
if(ch=='m'||ch=='n'||ch=='o')m[ch]=5;else
if(ch=='p'||ch=='q'||ch=='r'||ch=='s')m[ch]=6;else
if(ch=='t'||ch=='u'||ch=='v')m[ch]=7;else
if(ch=='w'||ch=='x'||ch=='y'||ch=='z')m[ch]=8;return
m[ch];}int
main(){char
ch[110];while(cin>>ch){int
i,count1=0;if(strlen(ch)==1){ cout<<char2num(ch[strlen(ch)-1])<<endl;}else{for(i=0;i<=strlen(ch)-2;i++){ char
a=ch[i],b=ch[i+1]; if(charmap(a)==charmap(b))
count1+=char2num(a)+2; else
count1+=char2num(a);}count1+=char2num(ch[strlen(ch)-1]);cout<<count1<<endl;}}return
0;}/************************************************************** Problem:
1079 User:
kaoyandaren123 Language:
C++ Result:
Accepted Time:30
ms Memory:1524
kb****************************************************************/ |
本文介绍了一种算法,用于计算在手机键盘上输入特定字符串所需的时间。考虑了按键位置及等待时间因素。
884

被折叠的 条评论
为什么被折叠?



