PAT-2019年冬季考试-甲级-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.

题意

给出26个大写字母的7×5矩阵,然后给出以回车结尾的字符串,输出大写字母组成的单词,如果中间有非大写字母的,当做是分割符,换行即可。但是最后一个单词后面不要换行了。

题眼

1.7×5矩阵
要用26×7×5的三维矩阵来存26个大写字母的图像
2.大写字母
头文件cctype中isupper()能判断是不是大写字母
3.字符串有非大写字母
这个非大写字母有可能是空格、回车等转义字符,最好用scanf("%c")来读

陷阱

最烧脑子的部分是什么,你有想法吗?
就是打印的时候怎么表达出目前量对26×7×5的三维矩阵的转换。
我用的办法是:vector<int> word读进大写字母,然后写出转换的关系式:

w[i][j]=v[word[k]][i][j%6]
w放的是单词的图像,i为行,j为列
这里面还要注意的就是这个余6操作,能顺利地把每个字母都正确复制进去。

另一个陷阱就是换行代表了结束,和不允许最后有多余的空格或者换行。

代码

#include<iostream>
#include<cctype>
#include<vector>
int n,m;
char v[26][7][5];
int main(){
    for(int i=0;i<26;i++){
       for(int j=0;j<7;j++){
           scanf("%c%c%c%c%c\n",&v[i][j][0],&v[i][j][1],&v[i][j][2]
               ,&v[i][j][3],&v[i][j][4]);
        }
    }
    char ch;
    bool f=false;
    vector<int> word;
    while(~scanf("%c",&ch)){
        if(isupper(ch)){
            word.push_back(ch-'A');
        }else if(!word.empty()){
            if(f)printf("\n\n");
            int len=word.size()*6-1;
            char w[7][len];
            for(int i=0;i<7;i++){
                int k=0;
                for(int j=0;j<len;j++){
                    if(j>0&& (j+1)%6==0){
                        w[i][j]=' ';
                        k++;
                    }
                    else w[i][j]=v[word[k]][i][j%6];
                }
            }
            for(int i=0;i<7;i++){
                if(i!=0)printf("\n");
                for(int j=0;j<len;j++){
                    if(w[i][j]-' '!=0)printf("%c",w[i][j]);
                    else printf(" ");
                }
            }
            //if(ch=='\n')return 0;//写不写都行,这里没测试点
            f=true;
            word.clear();
        }
    }
    return 0;
}
发布了21 篇原创文章 · 获赞 1 · 访问量 1182

猜你喜欢

转载自blog.csdn.net/allisonshing/article/details/104100616
今日推荐