关于算法-数据结构:PAT甲级2019年冬季考试-71-Good-in-C

109次阅读

共计 4501 个字符,预计需要花费 12 分钟才能阅读完成。

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?

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.

题目限度:

题目粗心:

给你 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.

提交后果:

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;
}

正文完
 0