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 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的圖案,然后給出一串包含大寫字母的句子,把里面的大寫字母打印出來,每個單詞一行,單詞內的字母間空一列,行前行末沒有多余的空行和空格。
題解:
乍一眼看就覺得挺麻煩的就先跳過。之后做了很久很久,提交了無數次。。。
我先是把要打印的圖案忽略空行,先統一放在ans這個二維數組里,最后打印這個二維數組。
那么將圖案寫入ans中時,如下圖,要注意cc和rr的變化,沒打印一個字母,rr+=6,同時打印一列空行。遇到了非大寫字母的符號(注意:多個非大寫字母符號要看做一個),那就rr=1,cc+=7,同時一開始我只拿了13分,中間的三個測試點就過不了,原因可能是輸入的字符串可能包含空格,還可能因為沒有記錄每個大行(col)最多打印多少列,每個大行都是不一樣的,要記錄下來,然后注意一些小細節。這題我最后才過的,當時開心地哇了一聲~終於考完,100分啦,還驚到了旁邊的同學。。。hhh
AC代碼:
#include<bits/stdc++.h> using namespace std; char zm[30][15][15]; string s; char ans[10000][10000]; int rnum[505]; int main(){ for(int i=1;i<=26;i++){//存入zm中 for(int j=1;j<=7;j++){ for(int k=1;k<=5;k++){ cin>>zm[i][j][k]; } } } getchar(); getline(cin,s);//輸入的字符串可能會包含空格 int l=s.length(); int col=1,cc=1; int rr=1; int f=0;//連續幾個非大寫字母的符號看作一個符號 for(int i=0;i<l;i++){ if(s[i]>='A'&&s[i]<='Z'){ int lett=s[i]-'A'+1; f=1; for(int j=1;j<=7;j++){//打印一個字母到ans for(int k=1;k<=5;k++){ ans[cc+j-1][rr+k-1]=zm[lett][j][k]; rnum[col]=max(rnum[col],rr+k-1);//用於記錄每一大行最長有多長 } } rr+=6;//每打印一個字母在字母后面打印一個空行 for(int j=1;j<=7;j++){ ans[cc+j-1][rr-1]=' '; } }else{ if(f==1) {//如果符號之后又出出現過大寫字母了 cc+=7;//行數+7 col++;//大行數+1 rr=1; } f=0; } } if(f==0) col--;//最后一個符號不需要再多一行 for(int i=1;i<=col*7;i++){ for(int j=1;j<=rnum[(i-1)/7+1];j++){ cout<<ans[i][j]; } if(i!=col*7) cout<<endl;//行末無空格 if(i%7==0&&i!=col*7) cout<<endl;//沒打印7行空一行 } return 0; }