PAT(甲级)2019年冬季考试

PAT(甲级)2019年冬季考试

自我测试,两个小时多一点做完,99分,最后提交了才发现第二道题还有一个测试点没有过。。。。感觉比较简单吧,第一道题一开始审错了,浪费好多时间,先做后面三道,一个小时多一点做完,然后又花了一个小时做第一道题。。。。

7-1 Good in C

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.

题解

一开始审错题了,题目的意思是先输入26个字母的"c."字符组,然后再输入一行单词,然后让你输出单词的字符组。

有个坑就是最后面的单词记得用 g e t l i n e ( c i n , s t r ) getline(cin,str) getline(cin,str),因为可能会有空格的情况。

代码

#include<bits/stdc++.h>
using namespace std;
int main(){
	vector<string> vec[26];
	vector<int>w[50000];
	string str;
	for(int i=0;i<26;++i){
		for(int j=0;j<7;++j){
			cin>>str;
			vec[i].push_back(str);
		}
	}
	getline(cin,str);
	getline(cin,str);
	string world=str;
	int cnt=0;//第几层 
	for(int i=0;i<world.size();++i){
		if(world[i]>='A' && world[i]<='Z'){
			w[cnt].push_back(world[i]-'A');
		}
		else{
			if(w[cnt].size()!=0) cnt++;//换层 
		}
	}
	for(int i=0;i<=cnt;++i){
		if(w[i].size()==0) continue;
		for(int j=0;j<7;++j){
			for(int k=0;k<w[i].size();++k){
				cout<<vec[w[i][k]][j];
				if(k==w[i].size()-1) cout<<endl;
				else cout<<' ';
			}
		}
		if(w[i+1].size()!=0) cout<<endl;
	}
	
}

7-2 Block Reversing (25 分)

Given a singly linked list L. Let us consider every K nodes as a block (if there are less than K nodes at the end of the list, the rest of the nodes are still considered as a block). Your job is to reverse all the blocks in L. For example, given L as 1→2→3→4→5→6→7→8 and K as 3, your output must be 7→8→4→5→6→1→2→3.

Input Specification:
Each input file contains one test case. For each case, the first line contains the address of the first node, a positive N (≤10
​5
​​ ) which is the total number of nodes, and a positive K (≤N) which is the size of a block. The address of a node is a 5-digit nonnegative integer, and NULL is represented by −1.

Then N lines follow, each describes a node in the format:

Address Data Next
where Address is the position of the node, Data is an integer, and Next is the position of the next node.

Output Specification:
For each case, output the resulting ordered linked list. Each node occupies a line, and is printed in the same format as in the input.

Sample Input:
00100 8 3
71120 7 88666
00000 4 99999
00100 1 12309
68237 6 71120
33218 3 00000
99999 5 68237
88666 8 -1
12309 2 33218
Sample Output:
71120 7 88666
88666 8 00000
00000 4 99999
99999 5 68237
68237 6 00100
00100 1 12309
12309 2 33218
33218 3 -1

题解

好吧,不是水题,我还有一个测试点没有过呢-_-,咱也不知道为啥。

代码

#include<bits/stdc++.h>
using namespace std;
struct Node{
	int address,data,next,cnt;
}node[200000];
bool cmp(Node a,Node b){
	return a.cnt<b.cnt;
}
int main(){
	int head,n,k,cnt=0;
	cin>>head>>n>>k;
	for(int i=0;i<n;++i){
		int cur,val,next;
		cin>>cur>>val>>next;
		node[cur]=Node{cur,val,next};
	}
	vector<Node> vec;
	while(head!=-1){
		node[head].cnt=cnt++;
		vec.push_back(node[head]);
		head=node[head].next;
	}
	int i=0;
	while(i<n){
		if(i+k<=n) reverse(vec.begin()+i,vec.begin()+i+k);
		else reverse(vec.begin()+i,vec.end());
		i+=k;
	}
	reverse(vec.begin(),vec.end());
	for(int i=0;i<vec.size();++i){
		if(i<vec.size()-1) printf("%05d %d %05d\n",vec[i].address,vec[i].data,vec[i+1].address);
		else printf("%05d %d %d\n",vec[i].address,vec[i].data,-1);
	}
	
}

7-3 Summit (25 分)

A summit (峰会) is a meeting of heads of state or government. Arranging the rest areas for the summit is not a simple job. The ideal arrangement of one area is to invite those heads so that everyone is a direct friend of everyone.

Now given a set of tentative arrangements, your job is to tell the organizers whether or not each area is all set.

Input Specification:

Each input file contains one test case. For each case, the first line gives two positive integers N (≤ 200), the number of heads in the summit, and M, the number of friendship relations. Then M lines follow, each gives a pair of indices of the heads who are friends to each other. The heads are indexed from 1 to N.

Then there is another positive integer K (≤ 100), and K lines of tentative arrangement of rest areas follow, each first gives a positive number L (≤ N), then followed by a sequence of L distinct indices of the heads. All the numbers in a line are separated by a space.

Output Specification:

For each of the K areas, print in a line your advice in the following format:

  • if in this area everyone is a direct friend of everyone, and no friend is missing (that is, no one else is a direct friend of everyone in this area), print Area X is OK..
  • if in this area everyone is a direct friend of everyone, yet there are some other heads who may also be invited without breaking the ideal arrangement, print Area X may invite more people, such as H. where H is the smallest index of the head who may be invited.
  • if in this area the arrangement is not an ideal one, then print Area X needs help. so the host can provide some special service to help the heads get to know each other.

Here X is the index of an area, starting from 1 to K.

Sample Input:

8 10
5 6
7 8
6 4
3 6
4 5
2 3
8 2
2 7
5 3
3 4
6
4 5 4 3 6
3 2 8 7
2 2 3
1 1
2 4 6
3 3 2 1

Sample Output:

Area 1 is OK.
Area 2 is OK.
Area 3 is OK.
Area 4 is OK.
Area 5 may invite more people, such as 3.
Area 6 needs help.

题解

图的问题,不是很难,基本的dfs啥都没用到

代码

#include<bits/stdc++.h>
using namespace std;
int G[500][500],p[500];
bool isDirect(int k){
	for(int i=0;i<k;++i){
		for(int j=i+1;j<k;++j){
			if(G[p[i]][p[i+1]]==0) return false;
		}
	}
	return true;
}
int isNeibour(int k,int n,unordered_set<int> s){
	for(int i=1;i<=n;++i){
		if(s.find(i)!=s.end()) continue;
		int flag=0;
		for(int j=0;j<k;++j){
			if(G[i][p[j]]==0){
				flag=1;
				break; 
			}
		}
		if(flag==0){
			return i;
		}
	}
	return -1;
}
int main(){
	int n,m,k,q,a,b;
	cin>>n>>m;
	for(int i=0;i<m;++i){
		cin>>a>>b;
		G[a][b]=G[b][a]=1;
	}
	cin>>q;
	for(int i=1;i<=q;++i){
		cin>>k;
		unordered_set<int> s;
		for(int j=0;j<k;++j){
			cin>>p[j];
			s.insert(p[j]);
		}	
		if(!isDirect(k)) printf("Area %d needs help.\n",i);
		else{
			int index=isNeibour(k,n,s);
			if(index==-1) printf("Area %d is OK.\n",i);
			else printf("Area %d may invite more people, such as %d.\n",i,index);
		}
	}
	
}

7-4 Cartesian Tree (30 分)

A Cartesian tree is a binary tree constructed from a sequence of distinct numbers. The tree is heap-ordered, and an inorder traversal returns the original sequence. For example, given the sequence { 8, 15, 3, 4, 1, 5, 12, 10, 18, 6 }, the min-heap Cartesian tree is shown by the figure.

CTree.jpg

Your job is to output the level-order traversal sequence of the min-heap Cartesian tree.

Input Specification:

Each input file contains one test case. Each case starts from giving a positive integer N (≤30), and then N distinct numbers in the next line, separated by a space. All the numbers are in the range of int.

Output Specification:

For each test case, print in a line the level-order traversal sequence of the min-heap Cartesian tree. All the numbers in a line must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

10
8 15 3 4 1 5 12 10 18 6

Sample Output:

1 3 5 8 4 6 15 10 12 18

题解

这道题也不是很难,bfs做的,然后我是构造了一个node来存储找到的最小值所在的区间,因为要把它压入栈后,找它的左区间+右区间。

代码

#include<bits/stdc++.h>
using namespace std;
int in[100];
struct node{
	int start,end,index,val;
};
node findMin(int i,int j){
	if(i>j) return node{-1,-1,-1,-1};
	int minv=in[i],index=i;
	for(int u=i+1;u<=j;++u){
		if(in[u]<in[index]){
			minv=in[u],index=u;
		}
	}
	//cout<<1;
	return node{i,j,index,minv};
}
int main(){
	int n;
	cin>>n;
	for(int i=0;i<n;++i){
		cin>>in[i];
	}
	queue<node> que;
	node tmp=findMin(0,n-1);
	que.push(tmp);
	int flag=0;
	while(!que.empty()){
		if(flag==1) cout<<' ';
		cout<<que.front().val;
		flag=1;
		node u=findMin(que.front().start,que.front().index-1);
		node v=findMin(que.front().index+1,que.front().end);
		que.pop();
		if(u.end!=-1) que.push(u);
		if(v.end!=-1) que.push(v);
	}
}

=j;++u){
if(in[u]<in[index]){
minv=in[u],index=u;
}
}
//cout<<1;
return node{i,j,index,minv};
}
int main(){
int n;
cin>>n;
for(int i=0;i<n;++i){
cin>>in[i];
}
queue que;
node tmp=findMin(0,n-1);
que.push(tmp);
int flag=0;
while(!que.empty()){
if(flag==1) cout<<’ ';
cout<<que.front().val;
flag=1;
node u=findMin(que.front().start,que.front().index-1);
node v=findMin(que.front().index+1,que.front().end);
que.pop();
if(u.end!=-1) que.push(u);
if(v.end!=-1) que.push(v);
}
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值