• PAT-2019年冬季考试-甲级 7-1 Good in C (20分)


    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?

    HWC.jpg

    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;
    }
  • 相关阅读:
    模块(相当于Java里的包)
    if_else_while_for
    用户交互
    Python入门
    BigInteger类及方法应用
    selenium+java破解极验滑动验证码的示例代码
    Postman 使用详解
    Postman用法简介
    伟大架构师的秘密【转载】
    深入理解HTTP协议(转)
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/12005345.html
Copyright © 2020-2023  润新知