【PAT甲级】1164 Good in C (20 分)

一、题目分析

1. 翻译

PS:这是我的第一篇PAT题解笔记,所以会逐句翻译~

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.
对于每个测试用例,第一部分给出26个大写字母A-Z,每个字符按照7×5的“C”和“.”组成的矩阵给出。
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),
这个语句由几个单词构成(每个单词连续不超过10个大写英文字母),
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.
在输出的开头和末尾不能有多余的空行。

2. 关键点

1)注意句子、单词、字母的分别的格式要求。
句子是题目给出,单词是需要从句子中提取,不同单词以空行分隔,字母是将单词拆分,每个字母以空格分隔。
2)注意句子中不同单词之间的分隔符。
分隔符是除了大写字母之外任意字符,可以是!,~ 也可以是空格。
3)句子的可能不以大写字母开头,句子也可以是一个空句子。

二、代码解析

可能存在许多低级错误、基础知识的反思,我只是想记录一下,大佬们可以忽略~

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <set>
#include <map>
#include <string>
#include <cstring>
#include <cctype>
#include <unordered_map>
using namespace std;
int main() {
//	存储26个字母,每个字母由多个(行)string构成
	vector<string> v[26];
//	暂时存储输入的每行string
	string tempstr;

//	出错点:for(int i=26; i>0; i--) {
//	v[26]:v[0]-v[25]
	for(int i=0; i<26; i++) {
		for(int j=0; j<7; j++) {
//			cin读取数据遇到空格、换行、制表符会停止,而在句子的输入中分隔符有可能是空格、制表符,
//			所以不能用cin,而应该选择直接读取一行字符串的getline,格式为getline(cin,string)。
			getline(cin,tempstr);
//			存储第i个字母的第j行字符
			v[i].push_back(tempstr);
		}
	}
	string sen;
	vector<string> word;//存储拆分出来的单词
	getline(cin,sen);

//	空句子的特殊情况
	if(sen=="") return 0;

//	拆分单词
//	low是每个单词的开头,high是每个单词的结尾,i是定位
	int low=0,high=0,i=0;
//	出错点:while(sen[i++]!='\0')
//	seperate有可能是任何字符,包括'\0',后面可能还有单词,所以不能作为循环的结束
	while(i<sen.size()) {
		if(sen[i]<'A'||sen[i]>'Z') {
			i++;
		} else {
			low=i;
			while(i<sen.size()&&sen[i]>='A'&&sen[i]<='Z') {
				high=i;
				i++;
			}
//			出错点:tempstr=sen.substr(low,high);
//			substr函数参数使用错误,substr(position,number not terminal)
			tempstr=sen.substr(low,high-low+1);
			word.push_back(tempstr);
		}
	}

//	输出
	i=0;
	string temp;
	while(i<word.size()) {//第i个单词
//		一行一行输出,因为要每个字母之间空格分隔,
//		输出第i个单词的每个字母的第u行
		for(int u=0; u<7; u++) {//第u行
			for(int j=0; j<word[i].size(); j++) { //第i个单词的第j个字母
				int n=(int)(word[i][j]-'A');
				cout<<v[n][u];
				if(j!=word[i].size()-1) cout<<" ";
			}
			cout<<endl;
		}
		if(i!=word.size()-1) cout<<endl;
		i++;
	}
	return 0;
}

三、我的疑问

为什么getline(cin,tempstr) 不可以用cin>>tempstr 实现?getline(cin,sen) 是因为需要把sen的空格考虑进去,而tempstr仅仅是“C”和“.”构成的字符串,为什么不可以?
希望有路过的大佬可以帮忙解答一下~谢谢

如果喜欢我的博客,欢迎点赞、评论、收藏~~谢谢
(* ^ ▽ ^ *) ~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值