考試拿到了滿分但受考場狀態和知識水平所限可能方法不夠簡潔,此處保留記錄,仍需多加學習。備考總結(筆記目錄)在這裏html
"Forever number" is a positive integer A with K digits, satisfying the following constrains:node
Now you are supposed to find these forever numbers.ios
Each input file contains one test case. For each test case, the first line contains a positive integer \(N (≤5)\). Then N lines follow, each gives a pair of \(K (3<K<10)\) and \(m (1<m<90)\), of which the meanings are given in the problem description.git
For each pair of K and m, first print in a line Case X
, where X
is the case index (starts from 1). Then print n and A in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of n. If still not unique, output in the ascending order of A. If there is no solution, output No Solution
.express
2 6 45 7 80
Case 1 10 189999 10 279999 10 369999 10 459999 10 549999 10 639999 10 729999 10 819999 10 909999 Case 2 No Solution
#include<iostream> #include<vector> #include<algorithm> using namespace std; int k, m; char s[10]; bool issolved; struct Data{ int sumN; string data; }; vector<Data> result; bool cmp(Data a, Data b){ return a.sumN != b.sumN ? a.sumN < b.sumN : a.data < b.data; } bool isPrime(int n){ if (n <= 1) return false; for (int i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } int gcd(int a, int b){ return !b ? a : gcd(b,a%b); } int checkF(){ int A = stoi(s), B = A+1, sumA = 0, sumB = 0; string Bs = to_string(B); for (int i = 0; i < k; i++) sumA += s[i]-'0'; for (int i = 0; i < k; i++) sumB += Bs[i]-'0'; int d = gcd(sumA,sumB); if (d > 2 && isPrime(d)) return sumB; else return -1; } void DFS(int index, int digit, int sumD){ if (index > k - 1 || sumD > m) return; if (sumD + 9 * (k - index - 1) < m) return; s[index] = digit + '0'; if (sumD == m && index == k - 1){ if (checkF() >= 0){ issolved = true; string data = s; result.push_back({checkF(),data}); } return; } for (int i = 0; i < 10; i++) DFS(index+1, i, sumD+i); } int main() { int n; scanf("%d", &n); for (int i = 1; i < n + 1; i++){ scanf("%d%d", &k, &m); printf("Case %d\n", i); issolved = false; result.clear(); for (int j = 1; j < 10; j++) DFS(0,j,j); if (issolved){ sort(result.begin(), result.end(), cmp); for (int j = 0; j < result.size(); j++) cout << result[j].sumN << " " << result[j].data << endl; } else printf("No Solution\n"); } return 0; }
Given two singly linked lists \(L_1 = a_1 \to a_2 \to ... \to a_{n-1} \to a_n\) and \(L_2 = b_1 \to b_2 \to ... \to b_{m-1} \to b_m\). If \(n≥2m\), you are supposed to reverse and merge the shorter one into the longer one to obtain a list like \(a_1 \to a_2 \to b_m \to a_3 \to a_4 \to b_{m-1} ...\) For example, given one list being 6→7 and the other one 1→2→3→4→5, you must output 1→2→7→3→4→6→5.數組
Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of \(L_1\) and \(L_2\) , plus a positive \(N (\le 10^5)\) which is the total number of nodes given. The address of a node is a 5-digit nonnegative integer, and NULL is represented by -1
.函數
Then N lines follow, each describes a node in the format:post
Address Data Next
where Address
is the position of the node, Data
is a positive integer no more than \(10^5\), and Next
is the position of the next node. It is guaranteed that no list is empty, and the longer list is at least twice as long as the shorter one.學習
For each case, output in order the resulting linked list. Each node occupies a line, and is printed in the same format as in the input.this
00100 01000 7 02233 2 34891 00100 6 00001 34891 3 10086 01000 1 02233 00033 5 -1 10086 4 00033 00001 7 -1
01000 1 02233 02233 2 00001 00001 7 34891 34891 3 10086 10086 4 00100 00100 6 00033 00033 5 -1
#include<iostream> #include<vector> using namespace std; struct Node{ int address, data, next; } node[100001]; int main() { int head1, head2, n, address, data, next; scanf("%d%d%d", &head1, &head2, &n); for (int i = 0; i < n; i++){ scanf("%d%d%d", &address, &data, &next); node[address] = {address, data, next}; } vector<Node> list1, list2, result; for (int p = head1; p != -1; p = node[p].next) list1.push_back(node[p]); for (int p = head2; p != -1; p = node[p].next) list2.push_back(node[p]); if (list1.size() > list2.size()){ int j = list2.size() - 1; for (int i = 0; i < list1.size(); i = i + 2){ result.push_back(list1[i]); if (i + 1 < list1.size()) result.push_back(list1[i+1]); if (j >= 0) result.push_back(list2[j--]); } } else{ int j = list1.size() - 1; for (int i = 0; i < list2.size(); i = i + 2){ result.push_back(list2[i]); if (i + 1 < list2.size()) result.push_back(list2[i+1]); if (j >= 0) result.push_back(list1[j--]); } } for (int i = 0; i + 1 < result.size(); i++) printf("%05d %d %05d\n", result[i].address, result[i].data, result[i+1].address); printf("%05d %d -1\n", result[result.size()-1].address, result[result.size()-1].data); return 0; }
Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.
Each input file contains one test case. For each case, the first line gives a positive integer \(N (≤ 20)\) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the i-th line corresponds to the i-th node) in the format:
data left_child right_child
where data
is a string of no more than 10 characters, left_child
and right_child
are the indices of this node's left and right children, respectively. The nodes are indexed from 1 to N. The NULL link is represented by −1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.
For each case, print in a line the postfix expression, with parentheses reflecting the precedences of the operators.There must be no space between any symbols.
8 * 8 7 a -1 -1 * 4 1 + 2 5 b -1 -1 d -1 -1 - -1 6 c -1 -1
(((a)(b)+)((c)(-(d))*)*)
8 2.35 -1 -1 * 6 1 - -1 4 % 7 8 + 2 3 a -1 -1 str -1 -1 871 -1 -1
(((a)(2.35)*)(-((str)(871)%))+)
#include<iostream> using namespace std; struct Node{ string data; int lchild, rchild; } node[21]; bool occured[21] = {false}; void postorder(int root){ cout << "("; if (node[root].lchild == -1 && node[root].rchild != -1){ cout << node[root].data; postorder(node[root].rchild); cout << ")"; } else{ if (node[root].lchild != -1) postorder(node[root].lchild); if (node[root].rchild != -1) postorder(node[root].rchild); cout << node[root].data << ")"; } } int main() { int n, root; string data; scanf("%d", &n); for (int i = 1; i < n + 1; i++){ cin >> node[i].data >> node[i].lchild >> node[i].rchild; if (node[i].lchild != -1) occured[node[i].lchild] = true; if (node[i].rchild != -1) occured[node[i].rchild] = true; } for (root = 1; root < n + 1 && occured[root]; root++); postorder(root); return 0; }
Dijkstra's algorithm is one of the very famous greedy algorithms. It is used for solving the single source shortest path problem which gives the shortest paths from one particular source vertex to all the other vertices of the given graph. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years later.
In this algorithm, a set contains vertices included in shortest path tree is maintained. During each step, we find one vertex which is not yet included and has a minimum distance from the source, and collect it into the set. Hence step by step an ordered sequence of vertices, let's call it Dijkstra sequence, is generated by Dijkstra's algorithm.
On the other hand, for a given graph, there could be more than one Dijkstra sequence. For example, both { 5, 1, 3, 4, 2 } and { 5, 3, 1, 2, 4 } are Dijkstra sequences for the graph, where 5 is the source. Your job is to check whether a given sequence is Dijkstra sequence or not.
Each input file contains one test case. For each case, the first line contains two positive integers \(N_v(\le 10^3)\) and \(N_e(\le 10^5)\), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to \(N_v\)
Then \(N_e\) lines follow, each describes an edge by giving the indices of the vertices at the two ends, followed by a positive integer weight (≤100) of the edge. It is guaranteed that the given graph is connected.
Finally the number of queries, K, is given as a positive integer no larger than 100, followed by K lines of sequences, each contains a permutationof the \(N_v\) vertices. It is assumed that the first vertex is the source for each sequence.
All the inputs in a line are separated by a space.
For each of the K sequences, print in a line Yes
if it is a Dijkstra sequence, or No
if not.
5 7 1 2 2 1 5 1 2 3 1 2 4 1 2 5 2 3 5 1 3 4 1 4 5 1 3 4 2 5 3 1 2 4 2 3 4 5 1 3 2 1 5 4
Yes Yes Yes No
#include<iostream> using namespace std; const int INF = 0x3fffffff; int n, G[1001][1001], d[1001], query[1001]; bool Dijkstra(int root){ fill(d, d+1001, INF); bool vis[1001] = {false}; d[root] = 0; for (int i = 0; i < n; i++){ int u, min = INF; for (int j = 1; j < n + 1; j++) if (d[j] < min && !vis[j]) min = d[j]; if (d[query[i]] == min) u = query[i]; else return false; vis[u] = true; for (int j = 1; j < n + 1; j++) if (G[u][j] && !vis[j] && d[j] > d[u] + G[u][j]) d[j] = d[u] + G[u][j]; } return true; } int main() { int m, u, v, distance, k; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++){ scanf("%d%d%d", &u, &v, &distance); G[u][v] = G[v][u] = distance; } scanf("%d",&k); for (int i = 0; i < k; i++){ for (int j = 0; j < n; j++) scanf("%d", &query[j]); bool isD = Dijkstra(query[0]); printf("%s\n", isD ? "Yes" : "No"); } return 0; }