Topic Background Background
Now the use of mobile phones more and more widely
Title Description DescriptionThe general phone's keyboard is like this:
To press the English letter, you have to press the number key more. For example, to press X to press 92, the first will be a W,
And the second will turn W into X. 0 Click to make a space.
Your task is to read a few sentences containing only small letters and spaces in English, and find out what to say on the phone.
At least how many times the child needs to press the keyboard.
input/output format input/output
Input Format:
A single sentence, containing only English lowercase letters and spaces, and no more than 200 characters.
output Format:
An integer line that represents the total number of times that the keyboard was pressed.
input and Output sample sample Input/output
sample Test point # #
Input Sample:
I have a dream
Sample output:
23
Idea: This problem is relatively simple, you can first hit the table, this is relatively fast (record each button to press the number of times), enter only one character at a time, find the answer in the table plus counter can.
Note: input data has a number of sentences, one line, so you need to read in the file.
The code is as follows:
1#include <stdio.h>2#include <string.h>3 intans1[ -]={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,1};//make a table to count the number of times each key is required4 intPhoneChara[])5 {6 intans=0, I;7 for(i=0; I<strlen (a); i++) 8 {9 if(a[i]!=' ')//LettersTen { Oneans+=ans1[a[i]- the];//the counter plus the numbers in the table A } - Else //space, counter + + - { theans+=1; - } - } - returnans; + } - intMain () + { A Chara[201]; at gets (a); - while(A[strlen (a)-1]!=eof)//If the end of the file is not reached, the incoming function - { -printf"%d\n", Phone (a)); - gets (a); - } in return 0; -}
noip2010-Universal group Semi-finals simulation test-first question-Mobile phone