Two integers are called “friend numbers” if they share the same sum of their digits, and the sum is their “friend ID”. For example, 123 and 51 are friend numbers since 1+2+3 = 5+1 = 6, and 6 is their friend ID. Given some numbers, you are supposed to count the number of different friend ID’s among them.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer N. Then N positive integers are given in the next line, separated by spaces. All the numbers are less than .
Output Specification:
For each case, print in the first line the number of different friend ID’s among the given integers. Then in the second line, output the friend ID’s in increasing order. The numbers must be separated by exactly one space and there must be no extra space at the end of the line.
intcalFriendId(int s){ int sum = 0; do { sum += s % 10; s /= 10; } while (s); return sum; }
intmain(){ int n, cur; cin >> n; for (int i = 0; i < n; i++) { cin >> cur; int id = calFriendId(cur); vis[id] = true; } for (int i = 0; i < vis.size(); i++) if (vis[i]) ans.push_back(i); cout << ans.size() << endl << ans[0]; for (int i = 1; i < ans.size(); i++) cout << " " << ans[i]; return0; }
[1121] Damn Single (25)
Map
题目
“Damn Single (单身狗)” is the Chinese nickname for someone who is being single. You are supposed to find those who are alone in a big party, so they can be taken care of.
Input Specification:
Each input file contains one test case. For each case, the first line gives a positive integer , the total number of couples. Then lines of the couples follow, each gives a couple of ID’s which are 5-digit numbers (i.e. from 00000 to 99999). After the list of couples, there is a positive integer followed by M ID’s of the party guests. The numbers are separated by spaces. It is guaranteed that nobody is having bigamous marriage (重婚) or dangling with more than one companion.
Output Specification:
First print in a line the total number of lonely guests. Then in the next line, print their ID’s in increasing order. The numbers must be separated by exactly 1 space, and there must be no extra space at the end of the line.
intmain(){ int n, m, a, b; cin >> n; for (int i = 0; i < n; i++) { cin >> a >> b; companion[a] = b; companion[b] = a; } cin >> m; for (int i = 0; i < m; i++) { cin >> a; guest[a] = true; info.push_back(a); } for (auto p: info) { if (companion[p] == -1) { ans.push_back(p); } else { if (!guest[companion[p]]) ans.push_back(p); } } sort(ans.begin(), ans.end()); cout << ans.size(); if (ans.size()) printf("\n%05d", ans[0]); for (int i = 1; i < ans.size(); i++) printf(" %05d", ans[i]); return0; }
[1122] Hamiltonian Cycle (25)
图论
题目
The “Hamilton cycle problem” is to find a simple cycle that contains every vertex in a graph. Such a cycle is called a “Hamiltonian cycle”.
In this problem, you are supposed to tell if a given cycle is a Hamiltonian cycle.
Input Specification:
Each input file contains one test case. For each case, the first line contains 2 positive integers , the number of vertices, and , the number of edges in an undirected graph. Then lines follow, each describes an edge in the format Vertex1 Vertex2, where the vertices are numbered from 1 to . The next line gives a positive integer which is the number of queries, followed by lines of queries, each in the format:
where is the number of vertices in the list, and ’s are the vertices on a path.
Output Specification:
For each query, print in a line YES if the path does form a Hamiltonian cycle, or NO if not.
intmain(){ int n, m, v1, v2, k, cur, num; cin >> n >> m; G.resize(n + 1, vector<int>(n + 1, 0)); for (int i = 0; i < m; i++) { cin >> v1 >> v2; G[v1][v2] = 1; G[v2][v1] = 1; } cin >> k; for (int i = 0; i < k; i++) { bool flag = true; cin >> num; vector<int> v(num); set<int> s; for (int j = 0; j < num; j++) { cin >> v[j]; s.insert(v[j]); } if (s.size() != n || num != n + 1 || v[0] != v[num-1]) flag = false; for (int j = 0; j < num - 1; j++) if (!G[v[j]][v[j+1]]) flag = false; printf("%s\n", flag? "YES": "NO"); } return0; }
[1122] Is It a Complete AVL Tree (30)
AVL树
题目
An AVL tree is a self-balancing binary search tree. In an AVL tree, the heights of the two child subtrees of any node differ by at most one; if at any time they differ by more than one, rebalancing is done to restore this property. Figures 1-4 illustrate the rotation rules.
Now given a sequence of insertions, you are supposed to output the level-order traversal sequence of the resulting AVL tree, and to tell if it is a complete binary tree.
Input Specification:
Each input file contains one test case. For each case, the first line contains a positive integer . Then distinct integer keys are given in the next line. All the numbers in a line are separated by a space.
Output Specification:
For each test case, insert the keys one by one into an initially empty AVL tree. Then first print in a line the level-order traversal sequence of the resulting AVL tree. All the numbers in a line must be separated by a space, and there must be no extra space at the end of the line. Then in the next line, print YES if the tree is complete, or NO if not.