A题 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.
#include "bits/stdc++.h"
using namespace std;
map<char,vector<string>> ss;
void show(string& s){
for (int i = 0; i < 7; ++i) {
for(int j=0;j<s.size();j++){
cout << ss[s[j]][i];
if (j != s.size()-1) printf(" ");
}printf("\n");
}
}
int main(){
// freopen("input.txt","r",stdin);
for (int i = 0; i < 26; ++i) {
for (int j = 0; j < 7; ++j) {
string s; getline(cin,s);
ss[i+'A'].push_back(s);
}
}
string s; getline(cin,s);
string buffer;
for(int i=0;i<s.size();i++){
char e = s[i];
if (isupper(e)) {
buffer.push_back(e);
if (i == s.size()-1) {
show(buffer);
}
}else {
show(buffer),buffer.clear();
if (i != s.size() -1 ) printf("\n");
}
}
}
大致题意:每个字符用7*5的矩阵表示,用这些字符输出最后一行给出的words,每个word用不是英文字母的字符隔开,要求一行输出一个word。
其实很简单,对吧?当时写到这题的时候,一看到题目把我给吓坏了,从来没有看到过这么长的题目,然后就GG了。究其原因是一眼看上去很难,导致很难有认真看题目的耐心了,其实理解了题目意思之后会觉得很简单。
B题 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 (≤105) 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;
const int N = 100000;
struct Node{
int v{};
int next{-1};
};
Node nodes[N];
int main(){
// freopen("input.txt","r",stdin);
int head, n, k; cin >> head >> n >> k;
for (int i = 0; i < n; ++i) {
int addr; scanf("%d",&addr); scanf("%d%d",&nodes[addr].v,&nodes[addr].next);
}
int x = head;
vector<vector<int>> blocks;
vector<int> buffer;
while (x != -1){
buffer.push_back(x);
int next = nodes[x].next;
if (buffer.size() == k || next == -1){
blocks.push_back(buffer);
buffer.clear();
}
x = next;
}
reverse(blocks.begin(),blocks.end());
vector<int> ans;
for(auto &block:blocks) for(int t:block) ans.push_back(t);
for (int i = 0; i < ans.size(); ++i) {
if (i != ans.size()-1) printf("%05d %d %05d\n",ans[i],nodes[ans[i]].v,ans[i+1]);
else printf("%05d %d -1\n",ans[i],nodes[ans[i]].v);
}
}
按照“块”反转链表,这题很简单没什么说的。
C题 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.
#include "bits/stdc++.h"
using namespace std;
const int N = 210;
vector<int> links[N];
int main(){
// freopen("input.txt","r",stdin);
int n,m; cin >> n >> m;
for (int i = 0; i < m; ++i) {
int a,b; scanf("%d%d",&a,&b);
links[a].push_back(b); links[b].push_back(a);
}
int k; cin >> k;
for (int i = 0; i < k; ++i) {
int num; scanf("%d",&num);
map<int,int> in,cnt;
for (int j = 0; j < num; ++j) {
int id; scanf("%d",&id); in[id] = 1, cnt[id];
for(int fid:links[id]) cnt[fid] ++;
}
bool ok = true;
int invitedId = -1;
for(auto &item:cnt){
int id = item.first;
if (in.count(id) && item.second != num-1){
ok = false;
printf("Area %d needs help.\n",i+1);
break;
} else if (!in.count(id) && item.second==num && invitedId==-1){
invitedId = id;
}
}
if (ok && invitedId==-1) printf("Area %d is OK.\n",i+1);
else if (ok) printf("Area %d may invite more people, such as %d.\n",i+1,invitedId);
}
}
判断一个圈子是否两两之间互相都是朋友,或者还有人可以加入进来,或者不满足两两都是朋友。
D题 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.

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;
vector<int> a;
struct Node{
int v{};
Node* l{nullptr};
Node* r{nullptr};
};
Node* build(int l,int r){
if (l > r)
return nullptr;
int root = l;
for(int i=l;i<=r;i++){
if(a[i] < a[root]) root = i;
}
Node* node = new Node{a[root]};
node->l = build(l,root-1);
node->r = build(root+1,r);
return node;
}
void leverOrder(Node* node){
queue<Node*> q; q.push(node);
bool blank = false;
while (!q.empty()){
Node* p = q.front(); q.pop();
if (blank) printf(" %d",p->v);
else blank = true, printf("%d",p->v);
if (p->l != nullptr) q.push(p->l);
if(p->r != nullptr) q.push(p->r);
}
}
int main(){
// freopen("input.txt","r",stdin);
int n; cin >> n; a.resize(n);
for(int i=0;i<n;i++) scanf("%d",&a[i]);
Node* root = build(0,n-1);
leverOrder(root);
}
这是我去年在杭州考的第一次PAT考试。今年又报名了一次,希望能成功。
去年实在是太惨烈,因为不会调 -std=c++11 (后来才知道的),平时也是用的Clion,考场的Devc++和VS我都没有用过,导致在本地用不了 IDE!在整整一场考试中,代码几乎和“盲打”、“盲改”没有区别,大部分时间都没有花在思考上面。
本文分享了PAT考试中四道题目的解题思路及代码实现,包括使用C表示Hello World、按块反转链表、判断朋友圈子是否理想以及构建并遍历最小堆Cartesian树。
2019年冬季考试&spm=1001.2101.3001.5002&articleId=107532692&d=1&t=3&u=80bbd0b1d7d443b89f9a728bac073dff)
4050

被折叠的 条评论
为什么被折叠?



