PAT-2019年冬季考试-甲级-7-1 Good in C (20分)超详解,几招就满分通过

本题在2021年12月之后才会解封放到免费题库里,目前需要收费。

题目:

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 word读进大写字母,然后写出转换的关系式:

w[i][j]=v[word[k]][i][j%6]
w放的是单词的图像,i为行,j为列
这里面还要注意的就是这个余6操作,能顺利地把每个字母都正确复制进去。
另一个陷阱就是换行代表了结束,和不允许最后有多余的空格或者换行。

新版代码

只经过本地测试的

#include
#include
#include
using namespace std;
string a[26][7],str;
vector<int> word;
int main(){
    for(int i=0;i<26;i++){
        for(int j=0;j<7;j++){
            cin>>a[i][j];
        }
    }
    cin.ignore();//因为cin会把换行符留在缓冲区,所以这里要将换行符忽略掉
    getline(cin,str);
    bool flag=false;
    for(int i=0;i<str.size();i++){
        if(isupper(str[i]) && i==str.size()-1)
            word.push_back(str[i]-'A');
        if(isupper(str[i]) && i!=str.size()-1){
            word.push_back(str[i]-'A');
        }else{
            if(flag)cout<<endl<<endl;
            for(int k=0;k<7;k++){
                for(int j=0;j<word.size();j++){
                    if(j!=0)cout<<" ";
                    cout<<a[word[j]][k];
                }
                if(k!=6)cout<<endl;
            }
            word.clear();
            flag=true;
        }
    }
    return 0;
}

旧版代码

#include
#include
#include
using namespace std;
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;
}

备考汇总贴:2020年3月PAT甲级满分必备刷题技巧

如果喜欢我的博客,请为我点个赞,也欢迎多评论、转发,感谢!!

你可能感兴趣的:(PAT甲级)