zoukankan      html  css  js  c++  java
  • 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, NN 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;
    }


  • 相关阅读:
    PhotoshopCS6中文版图像处理实战从入门到精通
    Web安全开发指南
    OpenStack运维指南
    Word/Excel/PPT 2016高效办公实战从入门到精通
    UG NX 8.5中文版基础教程
    Moldflow 2018模流分析从入门到精通:升级版
    数据库与数据处理:Access 2010实现
    iOS开发网络数据之AFNetworking使用1
    AFNetworking2.5使用2
    iOS项目的完整重命名方法图文教程
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6971252.html
Copyright © 2011-2022 走看看