1164 Good in C (20 point(s)) PAT甲级

1164 Good in C (20 point(s))

字符串

题目

When your interviewer asks you to write “Hello World” using C, can you do as the following figure shows?

1164 Good in C (20 point(s)) PAT甲级

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.

思路

题意

用7*5的矩阵图形存储26个字母,读入一行句子,句子中单词以大写字母组成,由任意非大写字母分隔开。要求以矩阵图形输出该句子,每个单词一行,每个字母矩阵用空格隔开。

思路

一开始想的是用26*7*5的char矩阵来存储26个字母的矩阵图形,后来发现可以直接用26*7的string矩阵。具体做法都差不多,读入每个字母每行的表示方式,再读入需要输出的句子,用temp存储句子中当前读到的字母,Word存储单词,一个单词由一个字母index队列组成。temp-'A'即为字母的index,之后按照要求一行一行的输出即可。

难点主要在理清楚图形输出的顺序,思路清晰的话不是很难。

解法

#include<bits/stdc++.h>
using namespace std;

string Capital[26][7];
vector<int> Input;
vector<vector<int>> Word;

int main() {
	for (int i = 0; i < 26; i++) {
		for (int j = 0; j < 7; j++) {
			cin >> Capital[i][j];
		}
	}
	getchar();//跳过字母样例末尾换行符
	int cnt = 0;
	while (true) {
		char temp = getchar();
		if ('A' <= temp && temp <= 'Z') {
			Input.push_back(temp - 'A');//存入字母index
		}
		else if(!Input.empty()){//判断Input是否为空,防止读入连续个中止符号Word数错误增加(会影响最后换行符的输出)
			Word.push_back(Input);
			Input.clear();
		}
		if (temp == '\n')
			break;
	}
	for (int i = 0; i < Word.size(); i++) {
		for (int row = 0; row < 7; row++) {
			for (int j = 0; j < Word[i].size(); j++) {
				cout << Capital[Word[i][j]][row];
				if (j != Word[i].size() - 1) printf(" ");
			}
			printf("\n");
		}
		if (i != Word.size() - 1) printf("\n");
	}
	return 0;
}

注意

  • getchar()不会自动换行
  • cin每次读入一行,末尾的换行符会留下
上一篇:【leetcode】1647. Minimum Deletions to Make Character Frequencies Unique


下一篇:我想你需要看一下 前端开发常见的几种设计模式