2019年12月PAT甲级 第一题 Good in C(1164)题解

题目

When your interviewer asks you to write “Hello World” using C, can you do as the following figure shows?
2019年12月PAT甲级 第一题 Good in C(1164)题解_第1张图片
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.

解释

题目分为三步:

  1. 读入26个字母,我使用vector数组,每个大写字母对应7个string。
  2. 读入待打印的字符串,并进行拆分。拆分时需要注意两个地方(也是最后四分的地方):
      1) 开头非大写字母,那么就s.erase(s.begin());
      2) 两个有效的字符串之间的非大写字母数量大于1,那么就将t增加。
  3. 按照输出规则,每行输出的时候先将字符串相加,之后输出。两个有效的单词直接有空行输出。

正确代码

#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;
vector<string> v[26], str;
int main(){
    string s;
    for(int i = 0; i < 26; i++){
        for(int j = 0; j < 7; j++){
            getline(cin, s);
            v[i].push_back(s);
        }
    }
    getline(cin, s);
    if(s == "")return 0; 
    else{
	    int pos = 0, t = 0;
	    while(!isupper(s[0])) s.erase(s.begin());
	    while(t < s.length()){
	        while(isupper(s[t])) t++;
	        str.push_back(s.substr(pos, t - pos));
	        while(!isupper(s[t])) t++;
   	 	pos = t ;
    }
    for(int l = 0; l < str.size(); l++){
        if(l != 0) cout << endl;
            for(int j = 0; j < 7; j++){
            string temp;
                for(int i = 0; i < str[l].length(); i++){
        	        if(i != 0) temp += " ";
                	temp += v[str[l][i] - 'A'][j];
            	}
        	cout << temp << endl;
      		}
		}
	}
    return 0;
}

你可能感兴趣的:(PAT,PAT)