当前位置:网站首页>PAT乙级 1109 擅长C(详解)
PAT乙级 1109 擅长C(详解)
2022-07-27 12:48:00 【Curz酥】
当你被面试官要求用 C 写一个“Hello World”时,有本事像下图显示的那样写一个出来吗?

输入格式:
输入首先给出 26 个英文大写字母 A-Z,每个字母用一个 7×5 的、由 C 和 . 组成的矩阵构成。最后在一行中给出一个句子,以回车结束。句子是由若干个单词(每个包含不超过 10 个连续的大写英文字母)组成的,单词间以任何非大写英文字母分隔。
题目保证至少给出一个单词。
输出格式:
对每个单词,将其每个字母用矩阵形式在一行中输出,字母间有一列空格分隔。单词的首尾不得有多余空格。
相邻的两个单词间必须有一空行分隔。输出的首尾不得有多余空行。
输入样例:
..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!
输出样例:
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.
这是个稍微复杂的字符串处理题,得多花点时间熟悉下这种做法。
AC代码:
#include<bits/stdc++.h>
using namespace std;
string alpha[66][66]; //读入26个字母
string ans[6666]; //用来输出结果,注意空间要开大
string targetStr; //目标字符串
int main(){
for(int i = 0; i < 26; i++) //26个字母
for(int j = 0; j < 7; j++) //每个字母有7行,所以得存7行
getline(cin, alpha[i][j]); //依次一行一行地存
getline(cin, targetStr); //存目标串
int idx = 0; //结果数组的下标
int t = 0; //用来换行
bool f = false; //判断下一位是否是字母
for(int i = 0; i < targetStr.length(); i++){
if(targetStr[i] >= 'A' and targetStr[i] <= 'Z'){ //如果是字母
for(int j = 0; j < 7; j++){ //读取一个字母,因为一个字母有7行,所以读7次
ans[idx] += alpha[targetStr[i] - 'A'][j]; //[s[i] - 'A']代表得到的数范围在0到26,正好和字母分别对应上;[j]代表将读取到字母的第j行存在对应的res里
if(targetStr[i + 1] <= 'Z' and targetStr[i + 1] >= 'A') ans[idx] += " "; //如果目标串下一个还是字母,就加一个空格(因为结果每个字母之间都有空格)
++idx; //ans的下标idx向后一位,继续存取同一个字母7行中的下一行
}
f = true; //意味着当前是个字母,标记一下,等会读完一个单词后换行会起到作用
}
else if(f == true){ //能到达这里,说明当前字符不是字母,说明读完一个单词了
f = false; //重置
t += 7; //t+=7代表res下标往后移7位,相当于读完一个单词后换行
}
idx = idx % 7 + t; //同一个单词在一行,所以idx的下标要取余,否则输出的时候同个单词的字母就跑下面去了
}
for(int i = 0; ans[i].size() != 0; i++){ //ans从头开始向下一行一行输出,直到遇到空为止
if(i % 7 == 0 and i != 0) cout << "\n"; //读完一行单词,要换行
cout << ans[i] << "\n"; //想象成打印机,从上往下一行一行输出,所以要换行
}
return 0;
}
边栏推荐
- 关于2022年3月9日之后Typora登录不了--已解决
- Sff1004-mhchxm diode sff1004
- [cute new solution] Fibonacci sequence
- Redis distributed online installation
- W3School导航栏练习
- Initializing database error after reinstalling MySQL
- Why does the class annotated with @configuration generate cglib proxy?
- 592. 分数加减运算 : 表达式计算入门题
- [node+ts] build node+typescript project
- 银行案例|Zabbix跨版本升级指南,4.2-6.0不香吗?
猜你喜欢

Error: the source of the existing CacheManager is: urlconfigurationsource

文章复现:SRCNN

MySQL extensions

Map interface

B站713故障后的多活容灾建设|TakinTalks大咖分享

Overview of famous inner classes and anonymous inner classes

Minimally invasive brain science broke the listing: the company's market value is HK $14.3 billion, and minimally invasive medical is the major shareholder

JS true / false array conversion

ECCV2022 | RU&谷歌提出用CLIP进行zero-shot目标检测!

v-on基础指令
随机推荐
Error: slf4j: class path contains multiple slf4j bindings
Detail the execution process of JDBC query method
Forward pre check and reverse pre check
Do you really understand CMS garbage collector?
内涵语录
clearfix的作用
@Simple use of conditional
Gartner authority predicts eight development trends of network security in the next four years
爬虫
Gan: generate adversarial networks
W3School导航栏练习
Wfuzz, a test tool that can blur everything that can be blurred
延迟队列DelayQueue性能测试
How can the top 500 enterprises improve their R & D efficiency? Let's see what industry experts say!
clear
Baoli food listed on Shanghai Stock Exchange: annual revenue of 1.578 billion, market value of 5.8 billion
JS date and time format (year, month, day, hour, minute, second, week, quarter, time difference acquisition, date and timestamp conversion function)
Background and framework introduction and basic environment preparation of hucang integrated e-commerce project
接口测试实战教程01:接口测试环境搭建
Redis distributed online installation