zoukankan      html  css  js  c++  java
  • 九度oj 题目1079:手机键盘

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

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

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

    这破题要做出来还得去看看手机键盘,那不知道手机键盘的布置怎么办?
     1 #include <cstdio>
     2 #include <cstdlib>
     3 #include <cstring>
     4 
     5 char str[102];
     6 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};
     7 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};
     8 
     9 bool isOne(char a, char b) {
    10     int t1 = num[a-'a'];
    11     int t2 = num[b-'a'];
    12     if(t1 == t2) {
    13         return true;
    14     }
    15     return false;
    16 }
    17 
    18 int main(int argc, char const *argv[])
    19 {
    20     while(scanf("%s",str) != EOF) {
    21         int len = strlen(str);
    22         int wait = cnt[(str[0] - 'a')];
    23 
    24         for(int i = 1; i < len; i++) {
    25             if(isOne(str[i-1], str[i])) {
    26                 wait = wait + 2;
    27             }
    28             wait = wait + cnt[(str[i] - 'a')];
    29         }
    30         printf("%d
    ",wait);
    31     }    
    32     return 0;
    33 }
  • 相关阅读:
    算法每日一练
    golang 基础笔记
    python面试问题
    《高性能mysql》阅读笔记
    Day4 -- Most Frequent Subtree Sum
    Day3 -- Find Eventual Safe States
    Day2 -- Shifting Letters
    svn提交代码出现被锁住的情况(已解决)
    springboot 日常小bug:java.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4).
    如何使用ideal工具给朋友发邮件
  • 原文地址:https://www.cnblogs.com/jasonJie/p/5819810.html
Copyright © 2011-2022 走看看