zoukankan      html  css  js  c++  java
  • 每日一九度之 题目1079:手机键盘

    时间限制:1 秒

    内存限制:32 兆

    特殊判题:

    提交:2543

    解决:1401

    题目描述:
    按照手机键盘输入字母的方式,计算所花费的时间
    如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。
    如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下
    如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。
    现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。
    现在给出一串字符,需要计算出它所需要花费的时间。
    输入:

    一个长度不大于100的字符串,其中只有手机按键上有的小写字母

    输出:
    输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
    样例输入:
    bob
    www
    样例输出:
    7
    7

    模拟题,把手机键盘一打开,然后按着题目意思直接模拟就好。

    //Asimple
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #include <cctype>
    #include <cstdlib>
    #include <stack>
    #include <cmath>
    #include <set>
    #include <map>
    #include <string>
    #include <queue>
    #include <limits.h>
    #define INF 0x7fffffff
    using namespace std;
    const int maxn = 105;
    typedef long long ll;
    char str[maxn];
    int num[] = {2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9};
    int cnt[] = {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};
    
    bool isOne(char a, char b) {
        int t1 = num[a-'a'];
        int t2 = num[b-'a'];
        if(t1 == t2) {
            return true;
        }
        return false;
    }
    
    int main(){
        while(scanf("%s",str) != EOF) {
            int len = strlen(str);
            int wait = cnt[(str[0] - 'a')];
    
            for(int i = 1; i < len; i++) {
                if(isOne(str[i-1], str[i])) {
                    wait = wait + 2;
                }
                wait = wait + cnt[(str[i] - 'a')];
            }
            printf("%d
    ",wait);
        }    
        return 0;
    }
    低调做人,高调做事。
  • 相关阅读:
    silverlight的Datagrid控件列绑定属性笔记
    VC字符串处理整理
    Combobox实现多项选择 Silverlight下“Combobox”怎样实现多项选择?
    C# 类初始化顺序
    Silverlight程序设置断点无法进入调试的解决方案
    有哪些适合新手练手的Python项目?
    Ubuntu 终端常用命令
    浅析python 中__name__ = '__main__' 的作用
    py thon 多线程(转一篇好文章)
    python os.path模块
  • 原文地址:https://www.cnblogs.com/Asimple/p/5939015.html
Copyright © 2011-2022 走看看