三月pat(转)

转自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
版权声明:本文为博主原创文章,转载请附上博文链接!

猜你喜欢

转载自www.cnblogs.com/dayanjing/p/10466986.html
今日推荐