zoukankan      html  css  js  c++  java
  • 牛客网计算机考研复试-KY6-手机键盘

    题目链接:点这里


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


    思路:
    知道手机九键的排列方式,如图。

    注意7和9都有四个英文字母


    代码:

    #include <bits/stdc++.h>
    using namespace std;
    int ch[27];
    int t[27];
    void init(){
        for(int i=0;i<26;i++){
            ch[i] = i/3;
            t[i] = i%3+1;
        }
        ch[18]--; t[18]=4;
        t[19]=1;
        t[20]=2;
        ch[21]--; t[21]=3;
        t[22]=1;
        t[23]=2;
        ch[24]--; t[24]=3;
        ch[25]--; t[25]=4;
    //    for(int i=0;i<26;i++){
    //        cout << (char)(i+'a') << ch[i] << " " << t[i] << endl;
    //    }
    }
    int main(){
        init();
        string s;
        while(cin >> s){
            int ans = t[s[0]-'a'];
            for(int i=1;i<s.length();i++){
                int s1 = s[i]-'a';
                int s2 = s[i-1]-'a';
                if(ch[s1]==ch[s2]){
                    ans += 2;
                }
                ans += t[s1];
            }
            cout << ans << endl;
        }
        return 0;
    }
    

    自己写的麻烦了,这个大佬的代码就很好。

    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
        int key[26] = {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};
        string str;
        while(cin>>str)
        {
            int count = key[str[0]-'a'];
            for(int i=1;i<str.size();++i)
            {
                count += key[str[i]-'a'];
                if(key[str[i]-'a']-key[str[i-1]-'a']==str[i]-str[i-1])//判断是否在同一个按键上
                    count+=2;
            }
            cout<<count<<endl;
        }
    }
    
  • 相关阅读:
    ArrayList removeRange方法分析
    LinkedHashMap源码分析(基于JDK1.6)
    LinkedList原码分析(基于JDK1.6)
    TreeMap源码分析——深入分析(基于JDK1.6)
    51NOD 2072 装箱问题 背包问题 01 背包 DP 动态规划
    51 NOD 1049 最大子段和 动态规划 模板 板子 DP
    51NOD 1006 最长公共子序列 Lcs 动态规划 DP 模板题 板子
    8月20日 训练日记
    CodeForces
    CodeForces
  • 原文地址:https://www.cnblogs.com/123-wind/p/14285428.html
Copyright © 2011-2022 走看看