2019冬季PAT甲级

第三次考甲级(算上上次复试),只有96分,退步了 /(ㄒoㄒ)/~~~(上次复试100)。前一个半小时过了后三题(倒着做的),第一题卡了一个半小时,一直格式错误 /(ㄒoㄒ)/~
在这里插入图片描述

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?

在这里插入图片描述

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.

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 s{
	int a,b,c;
};
s s[100011],ss[100011],sss[100011];
int main() 
{
	int p,n,k;
	cin>>p>>n>>k;
	for(int i=0;i<n;i++)
	{
		int x,y,z;
		cin>>x>>y>>z;
		s[x].a=x;
		s[x].b=y;
		s[x].c=z;
	}
	int pp=0;
	for(int i=p;i!=-1;i=s[i].c)
	{
		ss[pp].a=s[i].a;
		ss[pp].b=s[i].b;
		ss[pp++].c=s[i].c;
	}
	int f=pp%k;
	int q=0,fff=0;
	for(int i=pp-f;i>=0;i-=k)
	{
//		if(i==n)continue;
		for(int j=i;j<i+k;j++)
		if(j<pp)
		{
			sss[q].a=ss[j].a;
			sss[q].b=ss[j].b;
			sss[q++].c==ss[j].c;
			fff=1;
		}
	}
	if(fff==1)
	printf("%05d %d",sss[0].a,sss[0].b);
	for(int i=1;i<pp;i++)
	{
		printf(" %05d\n%05d %d",sss[i].a,sss[i].a,sss[i].b);
	}
	if(fff==1)
	cout<<" -1";
	return 0;
}

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.

刚开始没看太懂题,以为是并查集。。。后来改了,半小时AC。

#include <bits/stdc++.h>
using namespace std;
int a[201][201],n,m;
/*int ff(int a)
{
	if(a==f[a])return a;
	ff(f[a]);
}
void u(int a,int b)
{
	if(ff(a)!=ff(b))f[a]=b;
}*/
int main()
{
	/*for(int i=0;i<=200;i++)
	{
		f[i]=i;
	}*/
	cin>>n>>m;
	while(m--)
	{
		int x,y;
		cin>>x>>y;
		//u(x,y);
		a[x][y]=1;
		a[y][x]=1;
	}
	int k;
	cin>>k;
	for(int j=1;j<=k;j++)
	{
		int num;
		cin>>num;
		set<int>s,ss;
		int b[num];
		for(int i=0;i<num;i++)
		{
			
			cin>>b[i];
			//s.insert(ff(x));
			ss.insert(b[i]);
		}
		/*if(s.size()!=1)
		{
			cout<<"Area "<<j<<" needs help."<<endl;
		}*/
		/*else
		{
			vector<int>v;
			for(int i=1;i<=n;i++)
			{
				if(ff(i)==ff(x))
				{
					v.push_back(i);
				}
			}
			if(v.size()==num)
			{
				cout<<"Area "<<j<<" is OK."<<endl;
			}
			else
			{
				sort(v.begin(),v.end());
				for(int i=0;i<v.size();i++)
				{
					if(s.find(v[i])==s.end())
					{
						cout<<"Area "<<j<<" may invite more people, such as "<<v[i]<<"."<<endl;
						break;
					}
				}
			}
		}*/
		int f=0,p;
		for(int i=1;i<=n;i++)
		{
			if(ss.find(i)==ss.end()&&f==0)
			{
				int j;
				for(j=0;j<num;j++)
				{
					if(a[i][b[j]]==0)
					{
						break;
					}
				}
				if(j==num)
				{f=1;p=i;
				}
			}
			
		}
		
		int ff=0;
		for(int i=0;i<num;i++)
		{
			for(int j=0;j<num;j++)
			{
				if(i!=j)
				{
					if(a[b[i]][b[j]]==0)
					{
						ff=1;
					}
				}
			}
		}
		
		if(ff==1)
		{
			cout<<"Area "<<j<<" needs help."<<endl;
		}
		else if(f==1)
		{
			cout<<"Area "<<j<<" may invite more people, such as "<<p<<"."<<endl;
		}
		else
		{
			cout<<"Area "<<j<<" is OK."<<endl;
		}
	}
}

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.

在这里插入图片描述

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

给小顶堆前序遍历,输出层序遍历。

#include <bits/stdc++.h>
using namespace std;
int n,a[31];
struct s{
	int d,i,l;
};
s s[31];
int q=0;
int c(struct s a,struct s b)
{
	return a.i<b.i;
}
void f(int l,int r,int ll,int i)
{
	if(l>r)return;
	
	int min,p;
	for(int i=l;i<=r;i++)
	{
		if(i==l)
		{
			min=a[l];
			p=l;
		}
		else
		{
			if(min>a[i])
			{
				min=a[i];
				p=i;
			}
		}
	}
	
	s[q].d=a[p];
	s[q].l=ll;
	s[q++].i=i;
//	system("pause");
	if(p>l)
	{//cout<<l<<" "<<p-1<<endl;
	f(l,p-1,ll+1,2*i+1);
	}
	if(p<r)
	{//cout<<p+1<<" "<<l<<endl;
	f(p+1,r,ll+1,2*i+2);
	}
}
int main()
{
	cin>>n;int min,p;
	for(int i=1;i<=n;i++)
	{
		cin>>a[i];
		if(i==1){min=a[i];p=i;}
		else if(a[i]<min){min=a[i];p=i;}
	}
	s[q].d=a[p];
	s[q].l=0;
	s[q++].i=0;
	if(p>1)
	f(1,p-1,1,2*0+1);
	if(p<n)
	f(p+1,n,1,2*0+2);
	
	sort(s,s+n,c);for(int i=0;i<n;i++)
	{
		if(i!=0)cout<<" ";
		cout<<s[i].d;
	}
}
发布了73 篇原创文章 · 获赞 26 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/l503301397/article/details/103449738