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 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表示方式,再输入一行字符串,该字符串以非大写字母的其他字符分割单词。每个字母之间一个空格,单词与单词之间一个空行。每行前后、每个单词上下无多余空格。
解题思路:这道题写的本来很顺!! 写完直接丢进去,心想这就满了,结果一过测试就13分。踩过的坑有:
1.逐行输入字符串的方式是啥?
getline(cin,string)
被我忘记了,硬试出来的……
2.大小写转换的方式是啥?(虽然这题没考到)
transform(s.begin(),s.end(),s.begin(),::tolower);
这个没用过,用字符硬换的】
3.判断是否为大写字符的方式?
我感觉最简单的办法可能就是 < A || > Z
了……
4.循环的边界问题,我太容易越界了……
5.开大数组多大算大?有个测试点是show[200]没过,show[20000]就过了,所以还是要敢于开个大的……
AC代码:
#include <iostream>
#include <map>
#include <algorithm>
#include <string>
#include <vector>
#include <cmath>
using namespace std;
vector<string> my;
vector<string> show[20000];
int main()
{
string str;
for (int i = 0; i < 182; i++) {
cin >> uppercase >> str;
my.push_back(str);
}
getchar();
string toshow;
getline(cin,toshow);
int line = 0;
int flag = 0;
for (int i = 0; i != toshow.length(); i++) {
if ( toshow.at(i) < 'A' || toshow.at(i) > 'Z' ) {
if (flag == 0 && show[0].size() != 0 ) {
line = line + 1;
}
flag = 1;
continue;
}
flag = 0;
for (int k = 0; k < 7; k++) {
int a = toshow.at(i) - 'A';
int pos = a*7 + k;
show[k+line*7].push_back(my[pos]);
}
}
for (int i = 0; i < (line+1)*7; i++) {
for (int k = 0; k < show[i].size() ; k++) {
cout << show[i][k];
if (k != show[i].size() - 1)
cout << " ";
else
cout << endl;
}
if ((i + 1) % 7 == 0 && show[i+1].size() != 0 )
cout << "\n";
}
return 0;
}
0 条评论