转自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. (63)Wangdao.com第十天_预处理、预解析_函数 上下文对象、参数列表对象

    预解析.预处理 1. 在全局代码执行之前,js 引擎 就会创建一个栈来存储管理所有的 执行上下文对象 2. 在 全局执行上下文 window 确定以后,进行压栈 3. 在 函数执行上下文对象 确定以后 ...

  2. asch相关的linux基础知识分享

    本文针对的人群:会用putty.SecureCRT.xhsell等工具ssh连接到自己的asch服务器上,但不怎么会执行命令的人.高手请绕路~ 本文主要围绕受托人搭建.维护涉及相关的内容进行Linux ...

  3. 洛谷P3388 【模板】割点(割顶)

    题目背景 割点 题目描述 给出一个n个点,m条边的无向图,求图的割点. 输入输出格式 输入格式: 第一行输入n,m 下面m行每行输入x,y表示x到y有一条边 输出格式: 第一行输出割点个数 第二行按照 ...

  4. 数位DP HDU - 2089 不要62

    不要62 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  5. js 高阶函数 闭包

    摘自  https://www.cnblogs.com/bobodeboke/p/5594647.html 建议结合另外一篇关于闭包的文章一起阅读:http://www.cnblogs.com/bob ...

  6. stm32通用定时器详解

    在stm32的开发中我们经常会用到定时器,因此在学习stm32的过程中定时器是必须要学的,而定时主要又分为三大类分别为: 高级控制定时器(TIM1与TIM8) 通用定时器(TIM2~TIM5) 基本定 ...

  7. poj1699

    #include<iostream> #include<cstring> using namespace std; ][]; ],len[],addlen[][]; int m ...

  8. Autofac之依赖注入

    这里主要学习一下Autofac的依赖注入方式 默认构造函数注入 class A { public B _b; public A() { } public A(B b) { this._b = b; } ...

  9. 关于VMware(虚拟机) 出现错误时处理办法

    我们在开发中难免会用到虚拟机.前段时间老有同学问我虚拟机报错解决办法,趁今天有空特随笔. 错误如下图 首先科普下VT是个啥? Intel VirtualTechnology(VT)既“虚拟化技术” V ...

  10. Spark入门到精通--(第七节)环境搭建(服务器搭建)

    Spark搭建集群比较繁琐,需要的内容比较多,这里主要从Centos.Hadoop.Hive.ZooKeeper.kafka的服务器环境搭建开始讲.其中Centos的搭建不具体说了,主要讲下集群的配置 ...