7-1 Sexy Primes (20 分)
Sexy primes are pairs of primes of the form (p, p+6), so-named since “sex” is the Latin word for “six”. (Quoted from http://mathworld.wolfram.com/SexyPrimes.html)

Now given an integer, you are supposed to tell if it is a sexy prime.

Input Specification:
Each input file contains one test case. Each case gives a positive integer N (≤108​​ ).

Output Specification:
For each case, print in a line Yes if N is a sexy prime, then print in the next line the other sexy prime paired with N (if the answer is not unique, output the smaller number). Or if N is not a sexy prime, print No instead, then print in the next line the smallest sexy prime which is larger than N.

Sample Input 1:
47
Sample Output 1:
Yes
41

Sample Input 2:
21
Sample Output 2:
No
23

这题还挺水的。。题意大概是当n和n+6都是素数时被称作性感素数~~输入一个n,判断他是否为性感素数,是则输出与他匹配的另一个数(n-6或n+6),输出较小的一个;如果不是,找到比n大的最小性感素数。
思路:一个判断素数的函数,在此基础上写一个判断性感素数的函数,是则返回与他匹配的另一个数,否则返回0。

#include<iostream>
using namespace std;
bool isPrime(int n){if(n<2)return 0;for(int i=2;i*i<=n;++i){if(n%i==0)return 0;}return 1;
}
int isSexyPrime(int n){if(isPrime(n)&&isPrime(n-6)) return n-6;else if(isPrime(n)&&isPrime(n+6)) return n+6;return 0;
}
int main(){int n;cin>>n;int m=isSexyPrime(n);if(m!=0){cout<<"Yes\n"<<m<<endl;}else{while(m==0) m=isSexyPrime(++n);cout<<"No\n"<<n<<endl;}return 0;
}

7-2 Anniversary (25 分)
Zhejiang University is about to celebrate her 122th anniversary in 2019. To prepare for the celebration, the alumni association (校友会) has gathered the ID’s of all her alumni. Now your job is to write a program to count the number of alumni among all the people who come to the celebration.

Input Specification:
Each input file contains one test case. For each case, the first part is about the information of all the alumni. Given in the first line is a positive integer N (≤10​5 ). Then N lines follow, each contains an ID number of an alumnus. An ID number is a string of 18 digits or the letter X. It is guaranteed that all the ID’s are distinct.

The next part gives the information of all the people who come to the celebration. Again given in the first line is a positive integer M (≤10​5​​ ). Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID’s are distinct.

Output Specification:
First print in a line the number of alumni among all the people who come to the celebration. Then in the second line, print the ID of the oldest alumnus – notice that the 7th - 14th digits of the ID gives one’s birth date. If no alumnus comes, output the ID of the oldest guest instead. It is guaranteed that such an alumnus or guest is unique.

Sample Input:
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042
Sample Output:
3
150702193604190912

题意浙大校庆有贵宾还是什么的,先给出n个贵宾的18位id,之后给出m个最后出席的id,先输出出席的贵宾个数,然后如果有贵宾出席,输出年龄最大的贵宾id,否则输出在场人中年龄最大的id,id的7-14位是出生日期。
思路:用ordered_set保存所有贵宾id;把出席的人根据集合放到两个不同的vector里面(这步有点多余,可以直接找年龄最大者的),然后通过stoi()比较得到年龄最大者。

#include<iostream>
#include<vector>
#include<unordered_set>
#include<string>
using namespace std;
unordered_set<string> st;
int main(){string s;int n,m;cin>>n;for(int i=0;i<n;++i){cin>>s;st.insert(s);}cin>>m;vector<string> a,b;for(int i=0;i<m;++i){cin>>s;if(st.find(s)!=st.end()) a.push_back(s);else b.push_back(s);}cout<<a.size()<<endl;if(a.empty()){int age=99999999;for(string &r:b){if(stoi(r.substr(7,8))<age){age=stoi(r.substr(7,8));s=r;}}cout<<s<<endl;}else{int age=99999999;for(string &r:a){if(stoi(r.substr(7,8))<age){age=stoi(r.substr(7,8));s=r;}}cout<<s<<endl;}return 0;
}

7-3 Telefraud Detection (25 分)
Telefraud(电信诈骗) remains a common and persistent problem in our society. In some cases, unsuspecting victims lose their entire life savings. To stop this crime, you are supposed to write a program to detect those suspects from a huge amount of phone call records.

A person must be detected as a suspect if he/she makes more than K short phone calls to different people everyday, but no more than 20% of these people would call back. And more, if two suspects are calling each other, we say they might belong to the same gang. A makes a short phone call to B means that the total duration of the calls from A to B is no more than 5 minutes.

Input Specification:
Each input file contains one test case. For each case, the first line gives 3 positive integers K (≤500, the threshold(阈值) of the amount of short phone calls), N (≤10​3​​ , the number of different phone numbers), and M (≤10​5​​ , the number of phone call records). Then M lines of one day’s records are given, each in the format:

caller receiver duration

where caller and receiver are numbered from 1 to N, and duration is no more than 1440 minutes in a day.

Output Specification:
Print in each line all the detected suspects in a gang, in ascending order of their numbers. The gangs are printed in ascending order of their first members. The numbers in a line must be separated by exactly 1 space, and there must be no extra space at the beginning or the end of the line.

If no one is detected, output None instead.
Sample Input 1:
5 15 31
1 4 2
1 5 2
1 5 4
1 7 5
1 8 3
1 9 1
1 6 5
1 15 2
1 15 5
3 2 2
3 5 15
3 13 1
3 12 1
3 14 1
3 10 2
3 11 5
5 2 1
5 3 10
5 1 1
5 7 2
5 6 1
5 13 4
5 15 1
11 10 5
12 14 1
6 1 1
6 9 2
6 10 5
6 11 2
6 12 1
6 13 1
Sample Output 1:
3 5
6
Note: In sample 1, although 1 had 9 records, but there were 7 distinct receivers, among which 5 and 15 both had conversations lasted more than 5 minutes in total. Hence 1 had made 5 short phone calls and didn’t exceed the threshold 5, and therefore is not a suspect.

Sample Input 2:
5 7 8
1 2 1
1 3 1
1 4 1
1 5 1
1 6 1
1 7 1
2 1 1
3 1 1
Sample Output 2:
None

这题算是最难的吧。题意大概是警察找嫌疑犯,当一个人与k个以上不同的人打短时间通话,且这些人中只有不超过20%的人给他打电话时,这个人就是嫌疑犯;相互有通话的嫌疑犯被认为是一组;按组内升序,组间按第一个组员升序的格式输出。短时间通话的定义为通话不超过5分钟。
思路:按照通话记录的建立有向图用邻接矩阵存储,第一轮遍历拿到每个人的短时间呼出次数和相应人员对他的的呼入次数;第二轮把满足呼出次数>k且呼入次数*5<呼出次数的放进vector;对vector进行DFS,把存在相互通话的嫌疑犯放进一个临时的vector里面,每次DFS后排序这个vector放到ans里面;排序ans并输出~~

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
int call[1111][1111]={0};
int in[1111]={0},out[1111]={0};
int k,n,m;
vector<int> temp;
vector<vector<int>> ans;
bool vis[1111]={0};
vector<int> temp1;
void DFS(int s){vis[s]=1;temp1.push_back(s);for(int &r:temp){if(!vis[r]&&call[s][r]&&call[r][s]) DFS(r);}
}
int main(){cin>>k>>n>>m;for(int i=0;i<m;++i){int x,y,d;cin>>x>>y>>d;call[x][y]+=d;}for(int i=1;i<=n;++i){for(int j=1;j<=n;++j){if(call[i][j]!=0&&call[i][j]<=5){++out[i];if(call[j][i]!=0)++in[i];}}}for(int i=1;i<=n;++i){if(out[i]>k&&out[i]>=in[i]*5){temp.push_back(i);}}sort(temp.begin(),temp.end());for(int &r:temp){if(!vis[r]) {temp1.clear();DFS(r);sort(temp1.begin(),temp1.end());if(!temp1.empty())ans.push_back(temp1);}}if(ans.empty()){cout<<"None"<<endl;return 0;}sort(ans.begin(),ans.end());for(int i=0;i<ans.size();++i){for(int j=0;j<ans[i].size();++j){if(j!=0) cout<<" ";cout<<ans[i][j];}cout<<endl;}return 0;
}

7-4 Structure of a Binary Tree (30 分)
Suppose that all the keys in a binary tree are distinct positive integers. Given the postorder and inorder traversal sequences, a binary tree can be uniquely determined.

Now given a sequence of statements about the structure of the resulting tree, you are supposed to tell if they are correct or not. A statment is one of the following:

A is the root
A and B are siblings
A is the parent of B
A is the left child of B
A is the right child of B
A and B are on the same level
It is a full tree
Note:

Two nodes are on the same level, means that they have the same depth.
A full binary tree is a tree in which every node other than the leaves has two children.

Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N (≤30), the total number of nodes in the binary tree. The second line gives the postorder sequence and the third line gives the inorder sequence. All the numbers in a line are no more than 10​3​​ and are separated by a space.

Then another positive integer M (≤30) is given, followed by M lines of statements. It is guaranteed that both A and B in the statements are in the tree.

Output Specification:
For each statement, print in a line Yes if it is correct, or No if not.

Sample Input:
9
16 7 11 32 28 2 23 8 15
16 23 7 32 11 2 28 15 8
7
15 is the root
8 and 2 are siblings
32 is the parent of 11
23 is the left child of 16
28 is the right child of 2
7 and 11 are on the same level
It is a full tree
Sample Output:
Yes
No
Yes
No
Yes
Yes
Yes

很烦的水题,不难但是要做好久,还容易搞错。
题意:输入二叉树的后序和中序,给出不同的陈述,判断他们的正确性。
思路:因为值都小于1000,所以用哈希来存树,保证最快的找到节点;这里用了字符串的输入流,其实直接cin也是没问题的(用sprintf更方便?当时没想到。。)~~ 直接读入第一个字符串,判断是不是“It”,是则用cin吸取剩下的字符串,输出fulltree?Yes:No;其他情况第一个都是数字,把刚才的字符串stoi一下,读入下一个字符串,判断lchild还是root等等,继续cin吸取掉没用的字符串,最后判断输出就好啦。

#include<iostream>
#include<vector>
#include<string>
#include<sstream>
using namespace std;
vector<int> in,post;
struct node{int key,level,father;int lchild=-1,rchild=-1;
};
node t[1111];
bool flag=0;
void create(int &r,int il,int ir,int pl,int pr,int level,int father){int i=il;while(in[i]!=post[pr-1]) ++i;r=in[i];t[r].father=father;t[r].level=level;int cnt=0;if(il<i){create(t[r].lchild,il,i,pl,pl+i-il,level+1,r);++cnt;}if(i+1<ir){create(t[r].rchild,i+1,ir,pl+i-il,pr-1,level+1,r);++cnt;} if(cnt==1) flag=1;
}
int main(){int n,m;cin>>n;in.resize(n);post.resize(n);for(int i=0;i<n;++i){cin>>post[i];}for(int i=0;i<n;++i){cin>>in[i];}int root;create(root,0,n,0,n,1,-1);cin>>m;getchar();while(m--){string s;getline(cin,s);istringstream is(s);is>>s;if(s=="It"){cout<<(flag?"No":"Yes")<<endl;}else{int x=stoi(s);is>>s;int y;if(s=="and"){is>>y;is>>s;is>>s;if(s=="on"){cout<<(t[x].level!=t[y].level?"No":"Yes")<<endl;}else if(s=="siblings"){    cout<<((t[x].father!=t[y].father||x==y)?"No":"Yes")<<endl;}}else{is>>s;is>>s;if(s=="root"){cout<<(x!=root?"No":"Yes")<<endl;}else if(s=="parent"){is>>s;is>>y;cout<<(t[y].father!=x?"No":"Yes")<<endl;}else if(s=="left"){is>>s;is>>s;is>>y;cout<<(t[y].lchild!=x?"No":"Yes")<<endl;}else if(s=="right"){is>>s;is>>s;is>>y;cout<<(t[y].rchild!=x?"No":"Yes")<<endl;}}}}return 0;
}

2019春季PAT题解相关推荐

  1. 2019春季PAT考试甲级答案

    20190302春季PAT考试甲级答案 7-1 Sexy Primes (20 分) Sexy primes are pairs of primes of the form (p, p+6), so- ...

  2. 2019秋季PAT甲级_C++题解

    2019 秋季 PAT (Advanced Level) C++题解 考试拿到了满分但受考场状态和知识水平所限可能方法不够简洁,此处保留记录,仍需多加学习.备考总结(笔记目录)在这里 7-1 Fore ...

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

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

  4. 2019春季学期期末总结

    2019春季学期期末总结作业 一.我学到的内容 二.我的收获 作业连接 收获 第二周作业 文件的建立 第三周作业 二维数组 第四周作业 选择排序法 第五周作业 字符串数组 第六周作业 指针 第七周作业 ...

  5. 2019春季学期第四周作业

    2019春季学期第四周作业 这个作业属于那个课程 C语言程序设计Ⅰ 这次作业要求在哪里 2019春季学期第四周作业 我在这个课程的目标是 我希望能够更加掌握循环和排序 参考文献 无 选择法排序 本题要 ...

  6. 2019 春季算法工程师实习生招聘历程

    持续了将近两个月的 2019 春季实习生招聘总算是告了一个段落,虽说去年入学时便已知道找工作就在眼前,但当它真正到来的时候,自己依然是措手不及.好在历经坎坷,结果总归是好的,希望接下来的实习收获满满. ...

  7. meta camp+21春季PAT乙级反思

    一.成绩: MetaCamp:一题未过,前两题过样例 ''''''''''21春季PAT乙级:49,前三题做完有个别例子没过,第四题没做'出'''来,'第'五'题'不会.第五题贪心算法不会,没有学过, ...

  8. OO第四单元小结暨2019春季学期OO课程总结

    OO第四单元小结暨2019春季学期OO课程总结 目录: OO第四单元总结 一.总结本单元两次作业的架构设计 第一次作业 第二次作业 学期最终总结 二.四个单元中架构设计及OO方法理解 2.1 第一单元 ...

  9. 2019春季中国餐厅周将携超过450家餐厅再度来袭

    2019年3月14日至3月24日,由DiningCity鼎食聚举办的2019春季中国餐厅周将在上海.北京.广州.深圳.成都.杭州.苏州七大城市再度来袭.超过450家精致餐厅都为这次活动准备了特别的餐厅 ...

  10. PAT(甲级)2019春季考试(Python实现)

    7-1 Sexy Primes 7-1 Sexy Primes 分数 20 作者 陈越 单位 浙江大学Sexy primes are pairs of primes of the form (p, p ...

最新文章

  1. 如何学习数据挖掘和数据科学的7个步骤
  2. 【c++】8.map和vector容器查找、删除指定元素、emplace、insert
  3. 从零开始学数据结构和算法(二)线性表的链式存储结构
  4. JVM调优之jstack找出最耗cpu的线程并定位代码
  5. Jn建站系统2.0源码 附视频安装教程
  6. 记,NSProxy需要实现哪些方法?
  7. idea安装lua插件_IntelliJ EmmyLua 1.1.9发布,IDEA的Lua插件
  8. 后台产品基本功:RBAC权限后台角色与权限设计
  9. Windows说明Linux分区和挂载点
  10. 自媒体是什么?三大媒体平台详细介绍,不了解的赶紧看着吧!
  11. android als传感器,环境光传感器(ALS)背光控制系统解决方案
  12. 怎样实现在微信中直接下载APK
  13. 漫画程序猿惯用口头禅
  14. 猫眼网历史日票房数据爬取
  15. Markdownpad2安装注册
  16. 用计算机打字用哪种方法最好,打字高手练习(快速练习好电脑打字的方法)
  17. D2 归来, 特此YC一篇
  18. 从电阻丝印读取电阻阻值
  19. 转一次排障经历以供学习
  20. ThreadPoolExecutor详解及线程池优化

热门文章

  1. API接口自动化测试框架搭建(二)-详细设计框架设计
  2. 评价指标MSE和AUC的参考文献
  3. document.getElementsByClassName的理想实现(@司徒正美 大神)
  4. iredmail邮箱使用
  5. cad批量打印_CAD如何进行批量打印图纸
  6. 怎么打开优酷的kux格式?教你把kux转换成mp4的方法
  7. mpa和pis_压力单位MPa/psi/bar之间的换算?
  8. linux常见通配符的含义,linux通配符含义
  9. win10sas安装教程_Android Studio详细安装教程
  10. springboot整合