2019.12.7PAT冬季考试第一题 - 7-1 Good in C (20分)

35 篇文章 0 订阅

第一题

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×5 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.

作者: 陈越

单位: 浙江大学

时间限制: 400 ms

内存限制: 64 MB

代码长度限制: 16 KB

 

题目大意:首先给出26个大写字母的字符表示形式,然后给出一个句子。要把这个句子用上述规定好的字符形式表示。每个字符之间空一列,每个单词之间空一行。

数据结构与算法。首先明确好数据存储的结构,然后对数据进行处理,地基一定要打好,不能着急。

1.自己的解法

首先要分别存储26个字母的特殊形式,每个字母是一个[7][5]的矩阵,一共26个字母,[26][7][5],三维矩阵太复杂了,放弃。

直接采用二维数组,将template读进来,则A是0~6行,B是7~13行,以此类推。存储好之后,要标准化需要输出的单词。有两种分割的条件,遇到非A~Z的字符,分割;到最后一个字符,分割。

自己的方法,16分。

// ConsoleApplication4.cpp : 定义控制台应用程序的入口点。
//

//#include "stdafx.h"
#include<cstdio>
#include<vector>
#include<iostream>
#include<queue>
#include<stack>
#include<cstring>
#include<string>
using namespace std;


vector<string> word;
int temp[15];
void OutputLetter(string s){
	int idx = 0;
	for(int i = 0; i < s.size(); i++){
		int start = (s[i] - 'A') * 7 + 1;
		temp[idx++] = start;
	}
	for(int i = 0; i < 7; i++){
		for(int j = 0; j < idx; j++){
			cout<<word[temp[j]++];
			if(j != idx - 1)
				printf(" ");
		}
		if(i!=6)cout<<endl;
	}

}
int main(){
	int n = 26;
	string s;
	string target;
	word.push_back("111");
	for(int i = 1; i <= 182; i++)
	{
		cin>>s;
		word.push_back(s);
	}
	getchar();
	getline(cin, target);
	vector<string> wait_string;
	string tmp;
	int begin = 0;
	for(int i = 0; i < target.size(); i++){
		if(target[i] >= 'A' && target[i] <= 'Z'){
		}
		else{
			tmp = target.substr(begin, i - begin );
			wait_string.push_back(tmp);
			begin = i + 1;
		}
	}
	if(begin != target.size()){
		tmp = target.substr(begin);
		wait_string.push_back(tmp);
	}
	for(int i = 0; i < wait_string.size(); i++){
		OutputLetter(wait_string[i]);
		if(i != wait_string.size() - 1)
			printf("\n\n");
	}
	
	return 0;
}

 

 

2.大神的解法

字符串的处理,substr

#include <cstdio>
#include <vector>
#include <algorithm>
#include <set>
#include <string>
#include <iostream>
using namespace std;
char letter[300][7][5];
void print(string str){
	for (int row=0; row<7; row++){
		for (int index=0; index<str.length(); index++){
			char let=str[index];
			for (int col=0; col<5; col++){
				printf("%c", letter[let][row][col]);
			}
			if (index!=str.length()-1) printf(" ");
		}
		printf("\n");
	}
}
int main(){
	for (int i='A'; i<='Z'; i++){
		for (int j=0; j<7; j++){
			for (int k=0; k<5; k++){
				scanf("%c",&letter[i][j][k]);
			}
			getchar();
		}
	}
	string str;
	getline(cin,str);
	vector<string> vt;
	int pre=0;
	int i=0;
	for (i=0; i<str.length(); i++){
		if (!(str[i]>='A' && str[i]<='Z')){
			string temp=str.substr(pre, i-pre);
			if (temp!="") vt.push_back(temp);
			pre=i;
			while (!(str[i]>='A' && str[i]<='Z')) {
				i++;
				pre++;
			}
		}
	}
	if (pre<str.length()) {
		string temp=str.substr(pre, str.length()-pre);
		vt.push_back(temp);
	}
	for (int i=0; i<vt.size(); i++){
		print(vt[i]);
		if (i!=vt.size()-1) printf("\n");
	}
}

3总结

  1. 学习cctype头文件。
  2. 不要怕程序上的麻烦,存储上的麻烦,逻辑清晰最重要,直接三维数组即可。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值