转自https://blog.csdn.net/weixin_40688413/article/details/88082779

担心别人删除了就找不到了。因为九月要考。

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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
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;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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;
}
---------------------
作者:空白wk
来源:CSDN
原文:https://blog.csdn.net/weixin_40688413/article/details/88082779
版权声明:本文为博主原创文章,转载请附上博文链接!

三月pat(转)的更多相关文章

  1. 《转载》PAT 习题

    博客出处:http://blog.csdn.net/zhoufenqin/article/details/50497791 题目出处:https://www.patest.cn/contests/pa ...

  2. PAT Judge

    原题连接:https://pta.patest.cn/pta/test/16/exam/4/question/677 题目如下: The ranklist of PAT is generated fr ...

  3. PAT/字符串处理习题集(二)

    B1024. 科学计数法 (20) Description: 科学计数法是科学家用来表示很大或很小的数字的一种方便的方法,其满足正则表达式[+-][1-9]"."[0-9]+E[+ ...

  4. PAT 1041. 考试座位号(15)

    每个PAT考生在参加考试时都会被分配两个座位号,一个是试机座位,一个是考试座位.正常情况下,考生在入场时先得到试机座位号码,入座进入试机状态后,系统会显示该考生的考试座位号码,考试时考生需要换到考试座 ...

  5. PAT 1040. 有几个PAT(25)

    字符串APPAPT中包含了两个单词"PAT",其中第一个PAT是第2位(P),第4位(A),第6位(T):第二个PAT是第3位(P),第4位(A),第6位(T). 现给定字符串,问 ...

  6. PAT 1032. 挖掘机技术哪家强(20)

    为了用事实说明挖掘机技术到底哪家强,PAT组织了一场挖掘机技能大赛.现请你根据比赛结果统计出技术最强的那个学校. 输入格式: 输入在第1行给出不超过105的正整数N,即参赛人数.随后N行,每行给出一位 ...

  7. pat甲级题解(更新到1013)

    1001. A+B Format (20) 注意负数,没别的了. 用scanf来补 前导0 和 前导的空格 很方便. #include <iostream> #include <cs ...

  8. PAT (Basic Level) Practise 1040 有几个PAT(DP)

    1040. 有几个PAT(25) 时间限制 120 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 字符串APPAPT中包含了两个单 ...

  9. PAT (Basic Level) Practise 1045 快速排序(离散化+主席树区间内的区间求和)

    1045. 快速排序(25) 时间限制 200 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CAO, Peng 著名的快速排序算法里有一个经典的划分 ...

随机推荐

  1. Vue重修02

    1.v-for的优先级比v-if/v-show都大 v-bind也可以绑定自定义的属性 2.父组件向子组件传值 <!DOCTYPE html> <html lang="en ...

  2. Restful概念的理解和践行

    在实习是leader有让我们实习生看restful相关的知识,奈何当初根基很浅,看了一篇博文,但是还是很难用自己的话来描述.现在又接触了Restful的内容,就补上一篇文章. 在Spring Fram ...

  3. thinkphp实现like模糊查询实例

    原网址:https://www.jb51.net/article/56846.htm

  4. Hibernate-day03

    双向的many2one/one2many 双向many2one/one2many的问题:一定有额外的SQL发出;在数据库里面,外键表示many2one或者one2many的关系,外键是没有方向的;但是 ...

  5. 使用ElasticSearch全文检索以及集群部署

    ElasticSearch  即 ES 安装:版本---elasticsearch-2.4.6.tar.gz tar -zxvf elasticsearch-2.4.6.tar.gz 由于es不允许使 ...

  6. webpack 知识点

    安装 webpack npm install -g webpack npm install -g webpack-cli@2.x 初始化项目 npm init -y npm install --sav ...

  7. 中国省份毗邻关系JSON数据[相邻省份][所辖市级信息][行政区划]

    最近做一个需求, 需要一份每个省份相邻[毗邻]的省份信息,这里整理了一版. json 数据,结构大致这样子的. [ { "id": 7, "name": &qu ...

  8. 现代IM系统中消息推送和存储架构的实现

    现代IM系统中消息推送和存储架构的实现-云栖社区-阿里云 https://yq.aliyun.com/articles/253242

  9. Confluo: Distributed Monitoring and Diagnosis Stack for High-speed Networks

    https://rise.cs.berkeley.edu/blog/confluo-millisecond-level-queries-on-large-scale-streaming-data/ht ...

  10. jeecg中excel导出字段判空处理

    我们清楚,jeecg 导出 excel 采用的是 easypoi,不知道是否遇到过这种情况: 我们以一个实体属性为例: @Excel(name="问题分类",dicCode=&qu ...