目录
7-4 Structure of a Binary Tree (30分)
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
题解:
sexy prime 满足条件:1.自身是素数 2.p-6或p+6有一个是素数即可。
#include <iostream>
using namespace std;
bool isPrime(int x) {
if (x <= 1) return false;
for (int i = 2; i <= x / i; i++)
if (x%i == 0)
return false;
return true;
}
int isSexy(int x) {
if (isPrime(x)) {
if (isPrime(x - 6)) return x - 6;
if (isPrime(x + 6)) return x + 6;
}
return -1;
}
int main() {
int a, b;
cin >> a;
if ((b = isSexy(a)) != -1) cout << "Yes\n" << b;
else {
while (isSexy(++a) == -1);
cout << "No\n" << a;
}
}
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 (≤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.
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
题解:
直接STL map做映射,查找,字符串截取即可
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
set<string> book;
vector<string> v;
bool cmp(string a, string b) {
string ba, bb;
ba = a.substr(6, 8), bb = b.substr(6, 8);
if (ba < bb) return true;
else return false;
}
int main() {
int n, m;
cin >> n;
for (int i = 0; i < n; i++)
{
string id;
cin >> id;
book.insert(id);
}
cin >> m;
for (int i = 0; i < m; i++)
{
string id;
cin >> id;
v.push_back(id);
}
sort(v.begin(), v.end(), cmp);
int cnt = 0;
string res = v[0];
for (int i = 0; i < m; i++)
{
if (book.count(v[i])) {
if (cnt == 0) res = v[i];
cnt++;
}
}
cout << cnt << '\n' << res;
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 (≤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.
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
题解:
这题有毒,给我做吐了,要仔细审题,注意是total
思路:有向图+并查集+map映射排序
不知道为啥用map自动排序一个点过不了,换成vector就过了···
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int N = 1e3 + 10;
int g[N][N];
int n, m, k;
int father[N];
vector<int> res;
map<int, vector<int>> gan;
int find(int x) {
if (father[x] != x) return find(father[x]);
return father[x];
}
int main() {
cin >> k >> n >> m;
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
g[a][b] += c;
}
for (int i = 1; i <= n; i++)
{
int cnts = 0, recall = 0;
for (int j = 1; j <= n; j++)
{
if (g[i][j] && g[i][j] <= 5)
{
cnts++;
if (g[j][i]) recall++;
}
}
if (cnts > k && (double)recall / cnts <= 0.2) res.push_back(i);
}
if (res.empty())
{
cout << "None\n";
return 0;
}
for (int i = 1; i < N; i++) father[i] = i;
for (int i = 0; i < res.size(); i++)
{
for (int j = i+1; j < res.size(); j++)
if (g[res[i]][res[j]] != 0 && g[res[j]][res[i]] != 0)
father[find(res[i])] = find(res[j]);
}
for (int i = 0; i < res.size(); i++)
{
gan[find(res[i])].push_back(res[i]);
}
vector <vector <int> > info;
for (auto it : gan)
{
sort(it.second.begin(), it.second.end());
info.push_back(it.second);
}
sort(info.begin(), info.end());
for (auto i : info)
{
cout << i[0];
for (int j = 1; j < i.size(); j++) cout << " " << i[j];
puts("");
}
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 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.
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
题解:
继续恶心人的题····重建树,字符串。
#pragma once
#include <iostream>
#include <string>
#include <unordered_map>
using namespace std;
const int N = 33;
int n, m, root;
int post[N], in[N];
bool isfull;
unordered_map<int, int> pos, depth, l, r, father;
int create(int il, int ir, int pl, int pr, int d) {
int root = post[pr];
depth[root] = d;
int k = pos[root];
if (k > il) {
int s = create(il, k - 1, pl, pl + k - 1 - il, d + 1);
l[root] = s;
father[s] = root;
}
if (k < ir) {
int s = create(k + 1, ir, (pr - 1) - (ir - k - 1), pr - 1, d + 1);
r[root] = s;
father[s] = root;
}
return root;
}
void check(string s) {
if (s.find("root") != string::npos) {
int num;
sscanf(s.c_str(), "%d is the root", &num);
root == num ? printf("Yes\n") : printf("No\n");
}
else if (s.find("siblings") != string::npos) {
int a, b;
sscanf(s.c_str(), "%d and %d are siblings", &a, &b);
if (father[a] == father[b])
printf("Yes\n");
else
printf("No\n");
}
else if (s.find("parent") != string::npos) {
int a, b;
sscanf(s.c_str(), "%d is the parent of %d", &a, &b);
if (father[b] == a)
printf("Yes\n");
else
printf("No\n");
}
else if (s.find("left child") != string::npos) {
int a, b;
sscanf(s.c_str(), "%d is the left child of %d", &a, &b);
if (l[b] == a)
printf("Yes\n");
else
printf("No\n");
}
else if (s.find("right child") != string::npos) {
int a, b;
sscanf(s.c_str(), "%d is the right child of %d", &a, &b);
if (r[b] == a)
printf("Yes\n");
else printf("No\n");
}
else if (s.find("level") != string::npos) {
int a, b;
sscanf(s.c_str(), "%d and %d are on the same level", &a, &b);
if (depth[a] == depth[b])
printf("Yes\n");
else printf("No\n");
}
else if (s.find("full") != string::npos) {
isfull ? printf("Yes\n") : printf("No\n");
}
}
bool DFS(int root) {
if ((!l.count(root) && r.count(root)) || (!r.count(root) && l.count(root)))
return false;
bool res1 = true, res2 = true;
if (l.count(root)) res1 = DFS(l[root]);
if (r.count(root)) res2 = DFS(r[root]);
return res1 && res2;
}
int main() {
cin >> n;
for (int i = 0; i < n; i++) cin >> post[i];
for (int i = 0; i < n; i++) {
cin >> in[i];
pos[in[i]] = i;
}
root = create(0, n - 1, 0, n - 1, 0);
isfull = DFS(root);
cin >> m;
getchar();
while (m--)
{
string str;
getline(cin, str);
check(str);
}
return 0;
}