清华大学2008年机试-手机键盘-1079

清华大学2008年机试-手机键盘-1079

题目描述:
按照手机键盘输入字母的方式,计算所花费的时间
如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。
如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下
如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。
现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。
现在给出一串字符,需要计算出它所需要花费的时间。
输入
一个长度不大于100的字符串,其中只有手机按键上有的小写字母
输出
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
样例输入
bob
www
样例输出
7
7
解题思路:
该题是一道简单模拟,没什么好讲的。

AC代码:

#include <cstdio>
#include <map>
#include <cstring>
const int maxn = 110;
const int space = 2;
struct Node{
    char ch;
    int button;
    int num;
}node[26];
char input[maxn];

void init(){
    for(int i = 0; i < 26; i++){
        node[i].ch = 'a' + i;
        if(i < 15){
            node[i].button = i/3;
            node[i].num = i%3 + 1;
        }
        else if(15 <= i && i < 19){
            node[i].button = 5;
            node[i].num = i - 14;
        }
        else if(19 <= i && i < 22){
            node[i].button = 6;
            node[i].num = i - 18;
        }
        else{
            node[i].button = 7;
            node[i].num = i - 21;
        }
    }
}

int cal(int len){
    int ans = 0;
    int index = input[0] - 'a';
    Node tmp = node[index];
    ans += tmp.num;
    for(int i = 1; i < len; i++){
        index = input[i] - 'a';
        Node it = node[index];
        ans += it.num;
        if(tmp.button == it.button) ans += space;
        tmp = it;
    }
    return ans;
}

int main(){
    init();
    //freopen("C:\\Users\\Administrator\\Desktop\\test.txt", "r", stdin);
    while(scanf("%s", input) != EOF){
        int len = strlen(input);
        printf("%d\n", cal(len));
    }
    //fclose(stdin);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值