首页 > 试题广场 >

手机键盘

[编程题]手机键盘
  • 热度指数:31570 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
按照手机键盘输入字母的方式,计算所花费的时间 如:a,b,c都在“1”键上,输入a只需要按一次,输入c需要连续按三次。 如果连续两个字符不在同一个按键上,则可直接按,如:ad需要按两下,kz需要按6下 如果连续两字符在同一个按键上,则两个按键之间需要等一段时间,如ac,在按了a之后,需要等一会儿才能按c。 现在假设每按一次需要花费一个时间段,等待时间需要花费两个时间段。 现在给出一串字符,需要计算出它所需要花费的时间。

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


输出描述:
输入可能包括多组数据,对于每组数据,输出按出Input所给字符串所需要的时间
示例1

输入

bob
www

输出

7
7
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNext()) {
            String str = in.next();//输入一串字符串
            char pre_char = str.charAt(0);
            int result = get_number(pre_char);
            if (str.length() == 1) {
                result = get_number(str.charAt(0));
                System.out.println(result);
                return;
            }
            for (int i = 1; i < str.length(); i++) {
                //先判断前后两个字符是否是同一个按键
                char t = str.charAt(i);
                int samePlace = in_the_same_place(pre_char, t);
                if (samePlace == 1) { //在不同位置
                    int after_num = get_number(t);
                    result = result + after_num;
                    pre_char = t;
                } else {//在同一位置
                    int pre_num = get_number(pre_char);
                    int after_num = get_number(t);
                    result = result + after_num + 2;
                    pre_char = t;
                }
            }
            System.out.println(result);
        }
    }
    public static int get_number(char c) {
        if (c == 'a' || c == 'd' || c == 'g' || c == 'j' || c == 'm' || c == 'p' ||
                c == 't' || c == 'w')
            return 1;
        else if (c == 'b' || c == 'e' || c == 'h' || c == 'k' || c == 'n' || c == 'q' ||
                 c == 'u' || c == 'x')
            return 2;
        else if (c == 'c' || c == 'f' || c == 'i' || c == 'l' || c == 'o' || c == 'r' ||
                 c == 'v' || c == 'y')
            return 3;
        else if (c == 's' || c == 'z')
            return 4;
        else
            return -1;
    }

    /**
     *
     * @param c1 前一个字符
     * @param c2 后一个字符
     * @return
     */
    public static int in_the_same_place(char c1,
                                        char c2) { //判断是否在同一个位置上
        //同一位置
        if ((c1 == 'a' || c1 == 'b' || c1 == 'c' ) && (c2 == 'a' || c2 == 'b' ||
                c2 == 'c'))
            return -1;
        else if ((c1 == 'd' || c1 == 'e' || c1 == 'f') && (c2 == 'd' || c2 == 'e' ||
                 c2 == 'f'))
            return -1;
        else if ((c1 == 'g' || c1 == 'h' || c1 == 'i' ) && (c2 == 'g' || c2 == 'h' ||
                 c2 == 'i'))
            return -1;
        else if ((c1 == 'j' || c1 == 'k' || c1 == 'l') && (c2 == 'j' || c2 == 'k' ||
                 c2 == 'l'))
            return -1;
        else if ((c1 == 'm' || c1 == 'n' || c1 == 'o') && ( c2 == 'm' || c2 == 'n' ||
                 c2 == 'o'))
            return -1;
        else if ((c1 == 'p' || c1 == 'q' || c1 == 'r' || c1 == 's') && (c2 == 'p' ||
                 c2 == 'q' || c2 == 'r' || c2 == 's'))
            return -1;
        else if ((c1 == 't' || c1 == 'u' || c1 == 'v') && ( c2 == 't' || c2 == 'u' ||
                 c2 == 'v'))
            return -1;
        else if ((c1 == 'w' || c1 == 'x' || c1 == 'y' || c1 == 'z') && (c2 == 'w' ||
                 c2 == 'x' || c2 == 'y' || c2 == 'z'))
            return -1;
        else
            return 1;//在不同位置

    }
}

发表于 2023-02-27 19:56:43 回复(0)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Main {
    static char[][] key = {{'a', 'b', 'c'}, {'d', 'e', 'f'}, {'g', 'h', 'i'}, {'j', 'k', 'l'}, {'m', 'n', 'o'}, {'p', 'q', 'r', 's'}, {'t', 'u', 'v'}, {'w', 'x', 'y', 'z'}};

    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String s;
        while ((s = br.readLine()) != null) {
            int re = 0;
            int[] xy = new int[2];
            int t = -1;
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                xy = Find(c);
                if (t == xy[0]) {//和上次的在一个按钮中
                    re += 2;
                }
                re += xy[1] + 1;
                t = xy[0];//记录
            }
            System.out.println(re);
        }

    }

    private static int[] Find(char c) {
        int[] xy = new int[2];
        for (int i = 0; i < key.length; i++) {
            for (int j = 0; j < key[i].length; j++) {
                if (key[i][j] == c) {
                    xy[0] = i;
                    xy[1] = j;
                }
            }
        }
        return xy;
    }

}


发表于 2021-04-13 21:38:07 回复(0)
import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String s = sc.next().trim();
            int num = getPushNum(s);
            System.out.println(num);
        }
        sc.close();
    }
    private static int getPushNum(String s){
        if(s == null || s.length() == 0) return 0;
        int[] group = new int[]{
                                1,1,1,
                                2,2,2,
                                3,3,3,
                                4,4,4,
                                5,5,5,
                                6,6,6,6,
                                7,7,7,
                                8,8,8,8};
        int[] time = new int[]{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};
        if(s.length() == 1){
            return time[s.charAt(0) - 'a'];
        }
        int res = 0;
        for(int i = 1; i < s.length(); i++){
            int pre = s.charAt(i - 1) - 'a';
            int cur = s.charAt(i) - 'a';
            if(i == 1) res += time[pre];
            if(group[pre] == group[cur]){
                res += 2;
            }
            res += time[cur];
        }
        return res;
    }
}

发表于 2020-08-11 20:25:35 回复(0)
//此方法思路为,创建String数组把按键枚举出来,利用String.indexOf()
//构造方法计算时间和判断是否在同一按键上
import java.util.Scanner;
public class Main{
//枚举按键,装入数组     public static String[] str = {"abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"};
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            char[] ch = sc.nextLine().toCharArray();  //接收键盘输入
            int times = cal(ch[0]);              //用tims接收计算第一个字符所需时间段
            for(int i=1;i<ch.length;i++){       //从第二个字符开始计算时间段
                if(judge(ch[i-1],ch[i]) > 0){   //判断连续两个字符是否在同一按键上
                    times+=2;                  //同一按键则时间段加2
                }
                times =cal(ch[i])+times;        //计算字符所需时间段
            }
            System.out.println(times);          //打印最终结果
        }
    }
 //创建cal()方法,计算按char c按键所需时间,返回index+1为按键所需时间段
    public static int cal(char c){
        int index = -1;
        for(int i=0;i<str.length;i++){
            index = str[i].indexOf(c);
            if(index != -1){
                return index+1;
            }
        }         return 0;
    }
 //判断字符char a和字符char b是否在同一按键上,是则返回1,否则返回-1
    public static int judge(char a,char b){
        int i=0,j=0;                    //用i记录a字符的按键位置,j记录b字符的按键位置
        for (; i < str.length; i++) {
            if(str[i].indexOf(a) != -1){
                break;
            }         }
        for (; j < str.length; j++) {
            if(str[j].indexOf(b) != -1){
                break;
            }         }
        if(i==j){                     //判断a字符和b字符是否在同一按键上
            return 1;
        }         return -1;
    }
}

编辑于 2018-08-04 07:31:30 回复(0)
简单、粗鲁、暴力:
import java.util.Objects;
import java.util.Scanner;

/**
 * Created by fhqplzj on 17-1-25 at 下午11:17.
 */
public class My3 {
    private static final String positions = "22233344455566677778889999";
    private static final int[] values = new int[]{
            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
    };

    private static int process(String s) {
        Objects.requireNonNull(s);
        s = s.toLowerCase();
        int n = s.length();
        if (n == 0) {
            return 0;
        }
        int result = values[s.charAt(0) - 'a'];
        for (int i = 1; i < n; i++) {
            if (positions.charAt(s.charAt(i - 1) - 'a') == positions.charAt(s.charAt(i) - 'a')) {
                result += 2;
            }
            result += values[s.charAt(i) - 'a'];
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextLine()) {
            String s = scanner.nextLine();
            System.out.println(process(s));
        }
    }
}

发表于 2017-01-26 08:48:16 回复(0)
import java.util.*;
public class Main{
    
    public static Data[] datas;
    public Main(){
        init();
        
    }
    public void init(){
        setData();
    
    }
   public void setData(){
       datas = new Data[26];
       for(int i=97;i<=122;i++){
           Data data = new Data();
           data.setKey((char)i);
           datas[i-97] = data; 
           setLoc(datas[i-97]);    
       }
    }
    
    
    public void setLoc(Data data){
            if(data.getKey()=='a'||data.getKey()=='b'||data.getKey()=='c'){
                 data.setLocation(2);
                 if(data.getKey()=='a'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='b'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='c'){
                     data.setPress(3);
                 }
            }
            if(data.getKey()=='d'||data.getKey()=='e'||data.getKey()=='f'){
                data.setLocation(3);
                 if(data.getKey()=='d'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='e'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='f'){
                     data.setPress(3);
                 }
            }
                 
            if(data.getKey()=='g'||data.getKey()=='h'||data.getKey()=='i'){
                data.setLocation(4);
                 if(data.getKey()=='g'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='h'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='i'){
                     data.setPress(3);
                 }
            }
                 
            if(data.getKey()=='j'||data.getKey()=='k'||data.getKey()=='l'){
                data.setLocation(5);
                 if(data.getKey()=='j'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='k'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='l'){
                     data.setPress(3);
                 }
            }
                
            if(data.getKey()=='m'||data.getKey()=='n'||data.getKey()=='o'){
                data.setLocation(6);
                 if(data.getKey()=='m'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='n'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='o'){
                     data.setPress(3);
                 }}
              
            if(data.getKey()=='p'||data.getKey()=='q'||data.getKey()=='r'||data.getKey()=='s'){
                data.setLocation(7);
                 if(data.getKey()=='p'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='q'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='r'){
                     data.setPress(3);
                 }
                if(data.getKey()=='s'){
                     data.setPress(4);
                 }
            }
                 
            if(data.getKey()=='t'||data.getKey()=='u'||data.getKey()=='v'){
                data.setLocation(8);
                 if(data.getKey()=='t'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='u'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='v'){
                     data.setPress(3);
                 }
            }
                 
            if(data.getKey()=='w'||data.getKey()=='x'||data.getKey()=='y'||data.getKey()=='z'){
                data.setLocation(9);
                 if(data.getKey()=='w'){
                     data.setPress(1);
                 }
                 if(data.getKey()=='x'){
                     data.setPress(2);
                 }
                 if(data.getKey()=='y'){
                     data.setPress(3);
                 } 
                 if(data.getKey()=='z'){
                     data.setPress(4);
                 }

            }
                
          
    }
    
    
    
    
    
    
    
    
    
    
    public static void main(String args[] ){
        Main m = new Main();
        Scanner ins = new Scanner(System.in );
        while(ins.hasNextLine()){
            String s = ins.nextLine();
            int time = 0;
             int flag = 1;
            for(int k = 0;k<s.length();k++){
               
                char c = s.charAt(k);
                
                for(int j =0;j<26;j++){
                    if(datas[j].getKey()==c){
                        if(flag==datas[j].getLocation()){
                            time+=2;
                         
                        }                  
                         time+=datas[j].getPress();
                         flag=datas[j].getLocation();
                         break;
                    }
                }
                
                
                
                
                
                
                
                
            }
            System.out.println(time);
        }
    }
}
 class Data{
    private char key;
    private int location;
    private int press;
    public void setLocation(int location){
        this.location=location;
    }
    public void setPress(int press){
        this.press=press;
    }
    public void setKey(char key){
        this.key=key;
    }
    public int getPress(){
        return press;
    }
    public int getLocation(){
        return location;
    }
   
    public char getKey(){
        return key;
    }
    public Data(){
        
    }
}
发表于 2016-08-13 18:11:38 回复(0)
//JAVA版本,注意下面几点就ok:1.pqrs和wxyz是4个字母一个键  2.其实就是把字符串每个字符累加,如果和前一个字符是同一个键,就+2 3.巧妙使用‘z’-'a'这种方式对应数组序数

import java.util.Scanner;

public class Main {

public static void main(String[] args){
Scanner in=new Scanner(System.in);
String line=null;
int group[]={
1,1,1,
2,2,2,
3,3,3,
4,4,4,
5,5,5,
6,6,6,6,
7,7,7,
8,8,8,8
};
int N[]={
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
};
Scanner sc = new Scanner(System.in);
         
         while(sc.hasNext()){
             
             line=sc.next();
             int first_gn=-1;
             int last_gn=-1;
             int n=-1;
             int m=-1;
             int count=0;
            // System.out.println("count:"+count);
             
             n=line.charAt(0)-'a';
             first_gn=group[n];
             count+=N[n];
             for(int i=1;i<line.length();i++){
             // System.out.println("count:"+count);
              
              
             m=line.charAt(i)-'a';
             last_gn=group[m];
             if(last_gn==first_gn){
             count+=2;
             // System.out.println("+2---count:"+count);
             }
             count+=N[m];
             //System.out.println("count:"+count);
             first_gn=last_gn;
             
             
             
             }
             System.out.println(count);
             
         }


        }
}

发表于 2016-06-28 01:21:37 回复(0)