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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
..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:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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个英文大写字母A-Z,每个字母用7×5的、由C和.组成的矩阵构成。最后在一行中给出一个句子,以回车结束。句子是由若干个单词(每个包含不超过10个连续的大写英文字母)组成的,单词间以任何非大写英文字母分隔。题目保证至少给出一个单词。输出要求:对于每个单词,将其中每个字母用矩阵形式在一行中输出,字母间有一列空格分隔。单词的首尾不得有多余空格。相邻的两个单词间必须有一空行分隔。输出的首尾不得有多余空行。
分析:三维数组a中储存26个字母对应的图形矩阵,out中储存每行要输出的图形矩阵,同时初始化out所有元素为空格。字符串中可能存在乱七八糟的字符,所以用getline做句子的输入。句子的单词间以任何非大写字母分隔,用while循环遍历找到下标j为非大写字母所在下标,i为当前单词首下标,然后根据单词中每一个字母,将输出图形记录到out中,最后输出out数组~flag用来标记之前是否输出过一个单词,如果输出过,flag=1,则当再次需要输出单词之前需要先输出一个’\n’~
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
#include <iostream> using namespace std; char a[26][7][5], out[7][100]; string s; int main() { for (int i = 0; i < 7; i++) for (int j = 0; j < 100; j++) out[i][j] = ' '; for (int i = 0; i < 26; i++) for (int j = 0; j < 7; j++) for (int k = 0; k < 5; k++) cin >> a[i][j][k]; getchar(); getline(cin, s); for (int i = 0, j, flag = 0; i < s.size(); i++) { j = i; while (j < s.size() && s[j] >= 'A' && s[j] <= 'Z') j++; if (i == j) continue; for (int k = i; k < j; k++) for (int l = 0; l < 7; l++) for (int m = 0; m < 5; m++) out[l][m + (k - i) * 6] = a[s[k] - 'A'][l][m]; if (flag) cout << '\n'; for (int k = 0; k < 7; k++) { flag = 1; for (int l = 0; l < 6 * (j - i) - 1; l++) cout << out[k][l]; cout << '\n'; } i = j; } return 0; } |
❤ 点击这里 -> 订阅《PAT | 蓝桥 | LeetCode学习路径 & 刷题经验》by 柳婼