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?
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个字母的矩阵,将它们记录在26个7×5的char数组中。
特别注意提取单词,“the words are separated by any characters other than capital English letters”,并且句子的开头不一定就是单词的开头,如aHellobWorldc
;
输出的方法,最外层循环是单词下标的循环,第二层循环是行数的循环(7行),第三层是一个单词的字母数的循环。注意控制好空格和换行,单词的末尾不能有多余的空行。
#include<string>
#include<algorithm>
#include<vector>
#include<map>
#include<iostream>
#include<cctype>
using namespace std;
char letter[26][7][6]; //26个字母,每个字母7行
vector<string> word; //要打印的单词
int main() {
for (int i = 0; i < 26; i++) {
for (int j = 0; j < 7; j++) {
scanf("%s\n", letter[i][j]);
}
}
string s;
getline(cin, s);
int index = 0;
//找到单词的开头
while (index < s.length() && !(isalpha(s[index]) && isupper(s[index]))) {
index++;
}
while (index < s.length()) {
int end = index;
while (end < s.length() && isalpha(s[end]) && isupper(s[end])) {
++end;
}
word.push_back(s.substr(index, end - index));
index = end;
while (index < s.length() && !(isalpha(s[index]) && isupper(s[index]))) {
index++;
}
}
for (int k = 0; k < word.size(); k++) {
if (k != 0) {
printf("\n\n");
}
string w = word[k];
for (int i = 0; i < 7; i++) {
if (i != 0) {
printf("\n");
}
for (int j = 0; j < w.length(); j++) {
if (j != 0) {
cout << " ";
}
cout << letter[w[j] - 'A'][i];
}
}
}
}