Phone keypad - cattle-off

Title Description

Letter input mode according to phone keypad, such as the time it takes to calculate: a, b, c are in the "1" key, simply press the input a first input c requires three consecutive press. If two consecutive characters are not the same key, you can press directly, such as: ad requires double-, kz need to press 6 if two successive characters on the same key, you will need some time between two buttons, such as ac, after press a, you have to wait a while to press c. Now suppose each time the need to spend a period of time, the time it takes to wait for two time periods. Now given a string of characters, we need to calculate the time it takes to.

Enter a description:

A string of length no greater than 100, some of which only the phone keypad lowercase

Output Description:

Inputs may include multiple sets of data, for each set of data, an output in a given time Input string required
Example 1

Entry

bob
www

Export

7
7

Problem-solving ideas

This problem does not look so good to do, a relationship began to try to find the alphabetical order and press the number keys, to no avail.

Later, the array subscripts directly linked to the alphabetical order of the number key pressed, so long as it traversed string.

You also need to determine the next character is not with the current character in the press on a button. The method of determination is to look at  the sequence of letters is equal to the difference between the difference key press times.

 1 #include <string.h>
 2 #include <stdio.h>
 3 #include <stdlib.h>
 4 
 5 int main()
 6 {
 7     char s[100];
 8     int array[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};
 9     while(scanf("%s",s)!=EOF)
10     {
11         int count = array[s[0] - 'a'];
12         for(int i=1;i<strlen(s);i++)
13         {
14             count  = count + array[s[i]-'a'];
15             if(s[i]-s[i-1] == array[s[i]-'a'] - array[s[i-1]-'a'] )
16                count = count+2;
17         }
18         printf("%d\n",count);
19     }
20 }

 

summary

The key topic is how to find a letter on the same key press.

Daniel looked at the code to know other people thought clever, but brush the problem has become more and more interesting, good fuel it!

 

 

Guess you like

Origin www.cnblogs.com/jiashun/p/newcode5.html