题目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...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*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 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

结题思路

每个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 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;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甲级 2019年冬季 题解相关推荐

  1. 【PAT】PAT甲级题库所有题解(持续更新中...)

    题解: 本文为导航页,一些希望刷PAT甲级的玩家可以来看看,我会持续更新所有题目的题解(取决于我做到哪儿了(doge)) 题号按照PAT官网给出的标注 题目: 链接 标签 1001 A+B Forma ...

  2. 【PAT甲级】2020冬季 PAT 甲级

    2020冬季 PAT 甲级记录 第一次参加PAT,本来九月份报名的时候是打算到十二月份的时候把乙级的题库刷完,然后甲级的题库刷一半,结果因为各种各样的事情(主要是懒又没坚持0.0)这次直到考前乙级才刷 ...

  3. pat甲级考试报名费_PAT(甲级)2019年冬季考试 题解

    总结写在前头: 考了字符串.链表(伪链表).图.树.考点比较均衡. 本次考试难度还行,需要较扎实的数据结构底子,对于字符串处理的技巧有一定的要求. 说句题外话,字符串是个重点,主要因为很多人会忽视字符 ...

  4. 2019年9月8日秋季PAT甲级题解A1163(7-4)Dijkstra Sequence

    2019年9月8日秋季PAT甲级题解 A1163(7-4)Dijkstra Sequence (30 分) #include<iostream> #include<vector> ...

  5. 2019秋季PAT甲级考试总结:努力+策略+运气

    鉴于这两天有很多网友联系我问这次考试的题解,所以我干脆就花点时间把C++题解整理出来了,见文末 经过一两个月的备战PAT,在今天终于画上了一个圆满的句号,取得了满分的成绩. 我是在南京的金陵科技学院考 ...

  6. PAT甲级1141 PAT Ranking of Institutions :[C++题解]结构体、排序、哈希表、结构体构造函数、结构体内写函数、排名

    文章目录 题目分析 题目来源 题目分析 来源:acwing 分析:和下面这题是一道题: PAT甲级1137 Final Grading:[C++题解]结构体.排序.哈希表.结构体构造函数.结构体内写函 ...

  7. PAT甲级1151 LCA in a Binary Tree (30 分):[C++题解]LCA、最低公共祖先、哈希表映射

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析: 和下面这道题几乎是同一题:PAT甲级1143 Lowest Common Ancestor (30 分):[C++题解]LCA.最低 ...

  8. PAT甲级1106 Lowest Price in Supply Chain:[C++题解]树、结点到根结点的距离、树形dp、记忆化搜索

    文章目录 题目分析 题目链接 题目分析 来源:acwing 分析:这道题是第三次做了. 和PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树.结点到根结点的 ...

  9. PAT甲级1090 Highest Price in Supply Chain:[C++题解]树、结点到根结点的距离、记忆化搜索、树形dp

    文章目录 题目分析 题目链接 题目分析 来源:acwing 和PAT甲级1079 Total Sales of Supply Chain:[C++题解] 树.结点到根结点的距离.树形dp.记忆化搜索是 ...

最新文章

  1. 通过Mellanox ConnectX NIC使用XDP加速
  2. python 接口数据驱动_python接口测试实例--数据驱动(程序与数据分离)
  3. myeclipse 清理项目缓存的几大步骤
  4. 微软的100道算法面试题(一)
  5. 【CKEditor】下载历史版本4.8.x
  6. python 访问 zookeeper
  7. python面向对象编程类的成员总结
  8. 兰州职称计算机中心,【兰州2013年职称计算机考试报名通知】- 环球网校
  9. 基于AWS的云备份容灾解决方案
  10. UVA10344 23 out of 5【暴力+DFS】
  11. 机器学习回顾篇(2):最小二乘法
  12. 湖南省公务员考试计算机专业,湖南省2018年普通高等学校对口招生考试计算机应用类专业综合知识试题...
  13. java socket聊天_java_基于Java Socket实现一个简易在线聊天功能(一),最近做了一个项目,其中有一 - phpStudy...
  14. LinkedIn登录界面模糊效果
  15. CAM350对比电路图方法
  16. DBeaver连接GBase数据库
  17. 中国重点流域已实现休禁渔制度全覆盖
  18. 一箭N雕:多任务深度学习实战
  19. 公司 | 四年狂奔,少儿编程准独角兽小码王的盈利逻辑是什么?
  20. EXCEL表格中数字金额很大时后面零很多,如何设置直接以万元为单位显示,不显示后面的零

热门文章

  1. 设置路由器自动拨号上网
  2. 利用变换矩阵求解机器人操作臂雅克比矩阵
  3. 美团/饿了么外卖cps红包返利裂变分销小程序源码(附0基础搭建教程)
  4. QFP PQFP LQFP TQFP封装形式及PCB详解!
  5. 递归案例 ---- 母牛生小牛
  6. Python背单词记单词小程序,可自定义词库,支持多种记忆模式,根据词义拼写、选择单词,根据词意选择单词
  7. 获取当前上市公司基本信息——tushare库及基本使用
  8. 【贪心 / 线段树模拟费用流增广】BZOJ4977 [Lydsy八月月赛] 跳伞求生
  9. 基于vue和ElementUI时间选择控件的封装
  10. 用scratch编写游戏-蹦床小姑娘