首页 > 代码库 > google在线测试练习题3
google在线测试练习题3
Problem
The Latin alphabet contains 26 characters and telephones only have ten digits on the keypad. We would like to make it easier to write a message to your friend using a sequence of keypresses to indicate the desired characters. The letters are mapped onto the digits as shown below. To insert the character B
for instance, the program would press22
. In order to insert two characters in sequence from the same key, the user must pause before pressing the key a second time. The space character ‘ ‘
should be printed to indicate a pause. For example, 2 2
indicates AA
whereas 22
indicates B
.
Input
The first line of input gives the number of cases, N. N test cases follow. Each case is a line of text formatted as
desired_message
Each message will consist of only lowercase characters a-z
and space characters ‘ ‘
. Pressing zero emits a space.
Output
For each test case, output one line containing "Case #x: " followed by the message translated into the sequence of keypresses.
Limits
1 ≤ N ≤ 100.
Small dataset
1 ≤ length of message in characters ≤ 15.
Large dataset
1 ≤ length of message in characters ≤ 1000.
#include<iostream> #include<fstream> using namespace std; int main() { int n_case; ifstream ifile("C-large-practice.in"); ofstream ofile("result_c2.txt"); ifile >> n_case; int reference[26] = {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}; int replace[26] = {2,22,222,3,33,333,4,44,444,5,55,555,6,66,666,7,77,777,7777,8,88,888,9,99,999,9999}; for(int i = 0; i <= n_case; i++) { char line[1002]; ifile.getline(line, 1002); string words(line); if(i == 0) continue; ofile << "Case #" << i << ": "; for(int i = 0; i < words.length(); i++) { if(words[i] == ' ') { if(i - 1 >= 0 && words[i - 1] == ' ') ofile << ' ' << 0; else ofile << 0; } else { if(i - 1 >= 0 && reference[words[i] - 'a'] == reference[words[i - 1] - 'a']) ofile << ' ' << replace[words[i] - 'a']; else ofile << replace[words[i] - 'a']; } } ofile << endl; } return 0; }
google在线测试练习题3