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.
Each input file contains one test case. Each case gives a positive integer N (≤108).
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.
47
Yes
41
21
No
23
解析:(1)判断素数
#include
#include
#include
using namespace std;
int n;
bool judge(int it) {
if (it <=1)return false;
for (int i = 2; i*i <= it; i++)
if (it%i == 0)return false;
return true;
}
int main() {
scanf("%d", &n);
if (judge(n - 6)&&judge(n)) { printf("Yes\n%d", n - 6); }
else if (judge(n + 6) && judge(n)) { printf("Yes\n%d", n + 6); }
else {
printf("No\n");
for (int i = n + 1; ; i++)
if (judge(i)&&(judge(i+6)||judge(i-6))) {
printf("%d", i);
break;
}
}
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.
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 (≤105). 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 (≤105). Then M lines follow, each contains an ID number of a guest. It is guaranteed that all the ID's are distinct.
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.
5
372928196906118710
610481197806202213
440684198612150417
13072819571002001X
150702193604190912
6
530125197901260019
150702193604190912
220221196701020034
610481197806202213
440684198612150417
370205198709275042
3
150702193604190912
解析:1.利用string类的集合set。
2.注意年龄越大,出生年月越小
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
setsset;
vectorwhoc, l2;
string str;
int n, m;
int main() {
//ios::sync_with_stdio(false);
scanf("%d", &n);
for (int i = 0; i < n; i++) {
cin >> str;
sset.insert(str);
}
scanf("%d", &m);
for (int i = 0; i < m; i++) {
cin >> str;
l2.push_back(str);
if (sset.count(str))whoc.push_back(str);
}
cout << whoc.size() << endl;
string smax = "99999999", maxid;
if (whoc.size() != 0) {
for (int i = 0; i < whoc.size(); i++) {
string tp = whoc[i].substr(6, 8);
if (tp < smax) {
smax = tp;
maxid = whoc[i];
}
}
}
else {
for (int i = 0; i < l2.size(); i++)
if (l2[i].substr(6, 8) < smax) {
smax = l2[i].substr(6, 8);
maxid = l2[i];
}
}
cout << maxid << 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.
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 (≤103, the number of different phone numbers), and M (≤105, 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.
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.
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
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.
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
None
注意:短通话指的是加起来的和小于5分钟。
思路:(1)直接利用数组求和
(2)遍历二维图,找出其中满足可疑条件的放入一个数组
(3)在嫌疑人中找到两两通话的,union操作
(4)找到属于一个gang的输出
#include
#include
#include
#include
#include
#include
#define ll long long
using namespace std;
int n, m, k, callmap[1005][1005] = { 0 }, f[1005] = {0};
void union_root(int a,int b) {
if (a == b)return;
else if (a < b)f[b] = a;
else f[a] = b;
}
int find_root(int i) {
if (f[i] == -1||f[i]==0)return i;
else return f[i] = find_root(f[i]);
}
vectorsuspect;
int main() {
scanf("%d %d %d", &k, &n, &m);
for (int i = 0; i < m; i++) {
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
callmap[a][b] += c;
}
for (int i = 1; i <= n; i++) {
int cnt = 0,callback=0;
for (int j = 1; j <= n; j++)
if (callmap[i][j] <= 5 && callmap[i][j] != 0) {
if (callmap[j][i] > 0)callback++;
cnt++;
}
if (cnt > k&&callback*5<=cnt) {
suspect.push_back(i);
f[i] = -1;
}
}
for(int i=0;i0&&callmap[b][a]>0)
union_root(find_root(a),find_root(b));
}
if (suspect.size() == 0)printf("None");
for (int i = 1; i <= n; i++)
if (f[i] == -1) {
printf("%d", i);
for (int j = 1; j <= n; j++)
if (find_root(j) == i&&j!=i)printf(" %d", j);
printf("\n");
}
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:
Note:
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 103 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.
For each statement, print in a line Yes
if it is correct, or No
if not.
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
Yes
No
Yes
No
Yes
Yes
Yes
思路:(1)找出不同指令的操作数和关键词进行区分,map映射
(2)读入时按字符读入,再行区分数字和单词,再寻找关键词。
(3)使用map进行中后建树,tree存其父节点,同时建树时判断是否为满树。
#include
#include
#include
#include
#include
#include
#include
#include