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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章