1

7-1 Good in C (20分)

When your interviewer asks you to write "Hello World" using C, can you do as the following figure shows?

HWC.jpg

Input Specification:

Each input file contains one test case. For each case, the first part gives the 26 capital English letters A-Z, each in a 7×5 matrix of C's and .'s. Then a sentence is given in a line, ended by a return. The sentence is formed by several words (no more than 10 continuous capital English letters each), and the words are separated by any characters other than capital English letters.
It is guaranteed that there is at least one word given.

Output Specification:

For each word, print the matrix form of each of its letters in a line, and the letters must be separated by exactly one column of space. There must be no extra space at the beginning or the end of the word.
Between two adjacent words, there must be a single empty line to separate them. There must be no extra line at the beginning or the end of the output.

Sample Input:

..C..
.C.C.
C...C
CCCCC
C...C
C...C
C...C
CCCC.
C...C
C...C
CCCC.
C...C
C...C
CCCC.
.CCC.
C...C
C....
C....
C....
C...C
.CCC.
CCCC.
C...C
C...C
C...C
C...C
C...C
CCCC.
CCCCC
C....
C....
CCCC.
C....
C....
CCCCC
CCCCC
C....
C....
CCCC.
C....
C....
C....
CCCC.
C...C
C....
C.CCC
C...C
C...C
CCCC.
C...C
C...C
C...C
CCCCC
C...C
C...C
C...C
CCCCC
..C..
..C..
..C..
..C..
..C..
CCCCC
CCCCC
....C
....C
....C
....C
C...C
.CCC.
C...C
C..C.
C.C..
CC...
C.C..
C..C.
C...C
C....
C....
C....
C....
C....
C....
CCCCC
C...C
C...C
CC.CC
C.C.C
C...C
C...C
C...C
C...C
C...C
CC..C
C.C.C
C..CC
C...C
C...C
.CCC.
C...C
C...C
C...C
C...C
C...C
.CCC.
CCCC.
C...C
C...C
CCCC.
C....
C....
C....
.CCC.
C...C
C...C
C...C
C.C.C
C..CC
.CCC.
CCCC.
C...C
CCCC.
CC...
C.C..
C..C.
C...C
.CCC.
C...C
C....
.CCC.
....C
C...C
.CCC.
CCCCC
..C..
..C..
..C..
..C..
..C..
..C..
C...C
C...C
C...C
C...C
C...C
C...C
.CCC.
C...C
C...C
C...C
C...C
C...C
.C.C.
..C..
C...C
C...C
C...C
C.C.C
CC.CC
C...C
C...C
C...C
C...C
.C.C.
..C..
.C.C.
C...C
C...C
C...C
C...C
.C.C.
..C..
..C..
..C..
..C..
CCCCC
....C
...C.
..C..
.C...
C....
CCCCC
HELLO~WORLD!

Sample Output:

C...C CCCCC C.... C.... .CCC.
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
CCCCC CCCC. C.... C.... C...C
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
C...C CCCCC CCCCC CCCCC .CCC.

C...C .CCC. CCCC. C.... CCCC.
C...C C...C C...C C.... C...C
C...C C...C CCCC. C.... C...C
C.C.C C...C CC... C.... C...C
CC.CC C...C C.C.. C.... C...C
C...C C...C C..C. C.... C...C
C...C .CCC. C...C CCCCC CCCC.

题目限制:

image.png

题目大意:

给你26个字母的7*5的图案,然后给出一串包含大写字母和其他字符的句子,把里面的大写字母打印出来,每个单词一行,单词内的字母间空一列,行前行末没有多余的空行和空格。

算法思路:

首先使用字符串数组s保存输入的所有A到Z的图像,那么对于任意大写字符@,int a=@-'A',其对应的图案为$7a$~$7a+6$。接着就是考虑如何输出其中的所有大写字母,由于每一个单词都得进行换行输出,这里采用按照单词进行分块,每一次输出一个单词然后换行输出下一个单词,那么就得在输入的字符串中提取出所需要的单词,这里使用string temp暂存当前保存的单词,初始为空,然后只要遇到A到Z就添加到temp中,否则只要temp非空就添加到单词集合result中,并重置temp为空,最后在循环退出的时候,如果temp非空,就将temp添加到result中即可。代码如下:

vector<string> result;
for(char i : r){
    if(i>='A'&&i<='Z'){
        temp += i;
    } else {
        // 两个单词之间有可能是多个非英文字母分隔的。 
        if(temp!=""){
            result.push_back(temp);
            temp = "";
        }
    }
}
// 最后有可能是英文字母结尾 
if(temp!=""){
    result.push_back(temp);
}

然后就是输出了,这里采用的输出方式为一行一行的输出,具体做法就是对于每一个单词,每一行遍历单词中的每一个字符,然后输出每一个字符图案的第一行,每输出7行就说明输出完一个单词进而换行输出下一个单重,知道result的单词全部输出完毕即可,代码如下:

for(int k=0;k<result.size();++k){
    // 对于每一个单词进行输出
    string word = result[k];
    for(int line=0;line<7;++line){
        // 每次输出line行的所有单词的字符串
        for(int i=0;i<word.size();++i){
            int a = word[i]-'A';
            //7*a+line就是需要每一个单词需要输出的行
            printf("%s",s[7*a+line].c_str());
            if(i<word.size()-1) printf(" ");
        }
        // 一行输出完毕,得回车
        printf("\n");
    }
    if(k<result.size()-1){
        // 不是最后一个单词
        printf("\n");
    }
}

注意点:

  • 1、测试点1,2考察单词之间有多个非大写字母的字符。
  • 2、测试点3,4考察单词最后是大写字母的情况,也就是没有非大写字母结尾。

测试数据:

Input(这里只给出最后的单词,针对测试点1,2):

HE@LLO!@#$WORLD!

Output:

C...C CCCCC
C...C C....
C...C C....
CCCCC CCCC.
C...C C....
C...C C....
C...C CCCCC

C.... C.... .CCC.
C.... C.... C...C
C.... C.... C...C
C.... C.... C...C
C.... C.... C...C
C.... C.... C...C
CCCCC CCCCC .CCC.

C...C .CCC. CCCC. C.... CCCC.
C...C C...C C...C C.... C...C
C...C C...C CCCC. C.... C...C
C.C.C C...C CC... C.... C...C
CC.CC C...C C.C.. C.... C...C
C...C C...C C..C. C.... C...C
C...C .CCC. C...C CCCCC CCCC.

Input(这里只给出最后的单词,针对测试点3,4):

HELLO~

Output:

C...C CCCCC C.... C.... .CCC.
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
CCCCC CCCC. C.... C.... C...C
C...C C.... C.... C.... C...C
C...C C.... C.... C.... C...C
C...C CCCCC CCCCC CCCCC .CCC.

提交结果:

image.png

AC代码:

#include<cstdio>
#include<string>
#include<iostream>
#include<vector>

using namespace std;

string s[183];

int main(){
    for(int i=0;i<182;++i){
        getline(cin,s[i]);
    }
    string r;
    getline(cin,r);
    string temp;
    vector<string> result;
    for(char i : r){
        if(i>='A'&&i<='Z'){
            temp += i;
        } else {
            // 两个单词之间有可能是多个非英文字母分隔的。 
            if(temp!=""){
                result.push_back(temp);
                temp = "";
            }
        }
    }
    // 最后有可能是英文字母结尾 
    if(temp!=""){
        result.push_back(temp);
    }
    for(int k=0;k<result.size();++k){
        // 对于每一个单词进行输出
        string word = result[k];
        for(int line=0;line<7;++line){
            // 每次输出line行的所有单词的字符串
            for(int i=0;i<word.size();++i){
                int a = word[i]-'A';
                //7*a+line就是需要每一个单词需要输出的行
                printf("%s",s[7*a+line].c_str());
                if(i<word.size()-1) printf(" ");
            }
            // 一行输出完毕,得回车
            printf("\n");
        }
        if(k<result.size()-1){
            // 不是最后一个单词
            printf("\n");
        }
    }
    return 0;
}

乔梓鑫
569 声望17 粉丝

主要分享个人学习经验和心得