糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > PAT甲级 冬季 题解

PAT甲级 冬季 题解

时间:2019-01-20 18:03:52

相关推荐

PAT甲级 冬季   题解

题目1: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...CCCCCCC...CC...CC...CCCCC.C...CC...CCCCC.C...CC...CCCCC..CCC.C...CC....C....C....C...CC.C...CC...CC...CC...CC...CCCC....C....CCCC.C....C....CCCCCCCCCCC....C....CCCC.C....C....C....CCCC.C...CC....CC...CC...CCCCC.C...CC...CC...CCCCCCC...CC...CC...CCCCCC..C....C....C....C....C..CCCCCCCCCC....C....C....C....CC...C.C...CC..C.C.C..CC...C.C..C..C.C...CC....C....C....C....C....C....CCCCCC...CC...CCC....CC...CC...CC...CC...CCC....CCC...CC...C.C...CC...CC...CC...CC...CC.C...CC...CCCCC.C....C....C.....CCC.C...CC...CC.....CC.C......C.C..C..C.C...C.C...CC.....CCC.....CC...CCC..C....C....C....C....C....C..C...CC...CC...CC...CC...CC...C.C...CC...CC...CC...CC...C.C.C...C..C...CC...CC...C...CC...CC...CC...C.C.C...C...C.C.C...CC...CC...CC...C.C.C...C....C....C....C..CCCCC....C...C...C...C...C....CCCCCHELLO~WORLD!

Sample Output

C...C CCCCC C.... C.... .CCC.C...C C.... C.... C.... C...CC...C C.... C.... C.... C...CCCCCC CCCC. C.... C.... C...CC...C C.... C.... C.... C...CC...C C.... C.... C.... C...CC...C CCCCC CCCCC CCCCC .CCC.C...C .CCC. CCCC. C.... CCCC.C...C C...C C...C C.... C...CC...C C...C CCCC. C.... C...CC.C.C C...C CC... C.... C... C...C C.C.. C.... C...CC...C C...C C..C. C.... C...CC...C .CCC. C...C CCCCC CCCC.

结题思路

用7*5的矩阵表示一个字符。题目会给你一行字符串,非大写字符作为分割的条件(可能包含空格,因此需要利用getline接收)

题目整体分为三步。第一初始化A-Z的表示矩阵。

第二,处理输入的字符串,按非大写字符分割成一个个单词

第三,每个单词作为整体进行输出

代码

#include <bits/stdc++.h>using namespace std;int e[520][520];//最多有200个人 int main(){int n,m,a,b;scanf("%d%d",&n,&m);fill(e[0],e[0]+520*520,-1);for(int i=0;i<m;i++){cin>>a>>b;e[a][b] = e[b][a] = 1;}int k;scanf("%d",&k);for(int i=1;i<=k;i++){int L,tmp;scanf("%d",&L);vector<int> v;map<int,int>has;for(int t=0;t<L;t++){cin>>tmp;v.push_back(tmp);has[tmp] = 1;//标记是否出现 }//判断任意两个人是不是朋友bool isAllFriend = true;for(int j=0;j<v.size();j++) {for(int u=j+1;u<v.size();u++){if(e[v[j]][v[u]] != 1){isAllFriend = false;break;}}}if(! isAllFriend){printf("Area %d needs help.\n",i);continue;}bool isok = true;for(int j=1;j<=n;j++){if(has[j] == 0){bool flag = true;for(int t = 0;t<v.size();t++){if(e[j][v[t]] != 1){flag = false;true;}}if(flag){printf("Area %d may invite more people, such as %d.\n",i,j);isok = false;break;}}}if(isok) printf("Area %d is OK.\n",i);} return 0; }

题目2: 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 371120 7 8866600000 4 9999900100 1 1230968237 6 7112033218 3 0000099999 5 6823788666 8 -112309 2 33218

Sample Output:

71120 7 8866688666 8 0000000000 4 9999999999 5 6823768237 6 0010000100 1 1230912309 2 3321833218 3 -1

结题思路

每个k作为整体进行反转。最后再整体进行反转,就能得到题目的效果。如题1→2→3→4→5→6→7→8

第一步,每k个进行反转得到

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

第二步,整体进行反转,得到

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

这些都可以直接调用库函数reverse直接做到。

代码

#include <bits/stdc++.h>using namespace std;struct node{int address;int key;int next;};//能够通过下标快速找到 node a[100010];int head;int n,k; int main(){scanf("%d%d%d",&head,&n,&k);int address,key,next;for(int i=0;i<n;i++){cin>>address>>key>>next;a[address].address = address;a[address].key = key;a[address].next = next;}vector<node> list1,list2;//按链表的顺序将节点放到集合中 for(int h = head;h!=-1;h = a[h].next){list1.push_back(a[h]); }//每k个进行反转for(int i=0;i<n;i+=k){reverse(list1.begin()+i,list1.begin()+min(i+k,n));} //整体再反转一次reverse(list1.begin(),list1.end()); int size = list1.size();for(int i=0;i<size-1;i++){printf("%05d %d %05d\n",list1[i].address,list1[i].key,list1[i+1].address);}printf("%05d %d %d\n",list1[size-1].address,list1[size-1].key,-1);return 0;}

题目3: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 105 67 86 43 64 52 38 22 75 33 464 5 4 3 63 2 8 72 2 31 12 4 63 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.

解题思路

用邻接矩阵存放边的关系,直接暴力判断就行。

代码

#include <bits/stdc++.h>using namespace std;int e[520][520];//最多有200个人 int main(){int n,m,a,b;scanf("%d%d",&n,&m);fill(e[0],e[0]+520*520,-1);for(int i=0;i<m;i++){cin>>a>>b;e[a][b] = e[b][a] = 1;}int k;scanf("%d",&k);for(int i=1;i<=k;i++){int L,tmp;scanf("%d",&L);vector<int> v;map<int,int>has;for(int t=0;t<L;t++){cin>>tmp;v.push_back(tmp);has[tmp] = 1;//标记是否出现 }//判断任意两个人是不是朋友bool isAllFriend = true;for(int j=0;j<v.size();j++) {for(int u=j+1;u<v.size();u++){if(e[v[j]][v[u]] != 1){isAllFriend = false;break;}}}if(! isAllFriend){printf("Area %d needs help.\n",i);continue;}bool isok = true;for(int j=1;j<=n;j++){if(has[j] == 0){bool flag = true;for(int t = 0;t<v.size();t++){if(e[j][v[t]] != 1){flag = false;true;}}if(flag){printf("Area %d may invite more people, such as %d.\n",i,j);isok = false;break;}}}if(isok) printf("Area %d is OK.\n",i);} return 0; }

题目4: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

1

2

Sample Output:

1 3 5 8 4 6 15 10 12 18

解题思路

给定小根堆的中序遍历的结果,要求输出层次遍历的结果、

根据小根堆的特性,值最小的就是根的值。每次都选择最小的值作为根节点,在递归的生成左右子树即可。

代码

#include <bits/stdc++.h>using namespace std;/*给定小根堆的中序遍历的结果,要求输出层次遍历的结果、根据小根堆的特性,值最小的就是根的值*/struct node{int key; struct node* left, *right; }; vector<int> in,ans;map<int,int>pos;//构造数 void build(node * &root,int inL,int inR){//cout<<"inL = "<<inL<<'\t'<<"inR = "<<inR<<endl;if(inL > inR)return;if(root == NULL){root = new node();root->left = NULL;root->right = NULL;}//cout<<"inL = "<<inL<<'\t'<<"inR = "<<inR<<endl;//找到区间[left,right]中值最小的int minkey = in[inL],mini = inL;for(int i=inL+1;i<=inR;i++){if(in[i] < minkey){minkey = in[i];mini = i;}} root->key = minkey;if(mini > inL)build(root->left,inL,mini-1);if(mini < inR)build(root->right,mini+1,inR);}void getPre(node *root){if(root == NULL)return;cout<<root->key<<'\t';getPre(root->left);getPre(root->right);}void getLevel(node *root){queue<node*>q;q.push(root);while(!q.empty()){//当q不为空node * tmp = q.front();// q.pop(); ans.push_back(tmp->key);if(tmp->left) q.push(tmp->left);if(tmp->right) q.push(tmp->right);}return;}int main(){int n;scanf("%d",&n);in.resize(n+1);for(int i=1;i<=n;i++){cin>>in[i];pos[in[i]] = i;//根据值快速找到位置。 }node *root = new node();build(root,1,n);getLevel(root);for(int i=0;i<ans.size();i++){cout<<ans[i];if(i != ans.size()-1)cout<<" ";}return 0; }

如果觉得《PAT甲级 冬季 题解》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。