charjindev@gmail.com charjindev

总分: 100 / 100

编程题 总分: 100 / 100


7-1 Forever 答案正确 得分: 20 / 20

"Forever number" is a positive integer AA with KK digits, satisfying the following constrains:

  • the sum of all the digits of AA is mm;
  • the sum of all the digits of A+1A+1 is nn; and
  • the greatest common divisor of mm and nn is a prime number which is greater than 2.

Now you are supposed to find these forever numbers.

Input Specification:

Each input file contains one test case. For each test case, the first line contains a positive integer NN (5\le 5). Then NN lines follow, each gives a pair of KK (3<K<103<K<10) and mm (1<m<901<m<90), of which the meanings are given in the problem description.

Output Specification:

For each pair of KK and mm, first print in a line Case X, where X is the case index (starts from 1). Then print nn and AA in the following line. The numbers must be separated by a space. If the solution is not unique, output in the ascending order of nn. If still not unique, output in the ascending order of AA. If there is no solution, output No Solution.

Sample Input:

2
6 45
7 80

Sample Output:

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 <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
using namespace std;
struct node {
    int n, v;
};

int n, k, m;
bool flag;
vector<node> ans;

bool cmp(node a, node b) {
    if (a.n != b.n) return a.n < b.n;
    else return a.v < b.v;
}

bool isPrime(int x) {
    if (x <= 1) return false;
    for (int i = 2; i * i <= x; i++) {
        if (x % i == 0) return false;
    }
    return true;
}

int gcd(int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

int getDigitSum(int x) {
    int sumD = 0;
    while (x != 0) {
        sumD += x % 10;
        x /= 10;
    }
    return sumD;
}

void dfs(int number, int numDigit, int sumD) {
    if (sumD > m) return;
    if (sumD + 9 * (k - numDigit) < m) return;
    if (numDigit == k) {
        if (sumD != m) return;
        int n = getDigitSum(number + 1);
        int x = gcd(m, n);
        if (x > 2 && isPrime(x)) {
            ans.push_back({n,number});
            flag = true;
        }
        return;
    }
    for (int i = 0; i <= 9; i++) {
        dfs(number * 10 + i, numDigit + 1, sumD + i);
    }
}

int main() {
    scanf("%d", &n);
    for (int i = 1; i <= n; i++) {
        printf("Case %d\n", i);
        scanf("%d%d", &k, &m);
        flag = false;
        ans.clear();
        for (int j = 1; j <= 9; j++) {
            dfs(j, 1, j);
        }
        if (ans.size() == 0) {
            printf("No Solution\n");
        } else {
            sort(ans.begin(), ans.end(), cmp);
            for (auto it : ans) {
                printf("%d %d\n", it.n, it.v);
            }
        }
    }
    return 0;
}
测试点 结果 耗时 内存
0 答案正确 3 ms 296KB
1 答案正确 2 ms 328KB
2 答案正确 4 ms 424KB
3 答案正确 3 ms 296KB

7-2 Merging Linked Lists 答案正确 得分: 25 / 25

Given two singly linked lists L1=a1a2an1anL_1 = a_1 \to a_2\to \cdots \to a_{n-1}\to a_n and L2=b1b2bm1bmL_2 = b_1 \to b_2\to \cdots \to b_{m-1}\to b_m. If n2mn\ge 2m, you are supposed to reverse and merge the shorter one into the longer one to obtain a list like a1a2bma3a4bm1a_1 \to a_2 \to b_{m} \to a_3 \to a_4 \to b_{m-1}\cdots . 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.

Input Specification:

Each input file contains one test case. For each case, the first line contains the two addresses of the first nodes of L1L_1 and L2L_2, plus a positive NN (105\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 NN lines follow, each describes a node in the format:

Address Data Next

where Address is the position of the node, Data is a positive integer no more than 10510^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.

Output Specification:

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.

Sample Input:

00100 01000 7
02233 2 34891
00100 6 00001
34891 3 10086
01000 1 02233
00033 5 -1
10086 4 00033
00001 7 -1

Sample Output:

01000 1 02233
02233 2 00001
00001 7 34891
34891 3 10086
10086 4 00100
00100 6 00033
00033 5 -1
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
const int maxn = 100010;
struct node {
    int adr, data, next;
} Node[maxn];
vector<node> l1, l2, ans;
void generateList(vector<node> &L, int head) {
    int p = head;
    while (p != -1) {
        L.push_back(Node[p]);
        p = Node[p].next;
    }
}

void printList(vector<node> L) {
    if (L.size() == 0) return;
    for (int i = 0; i < L.size() - 1; i++) {
        printf("%05d %d %05d\n", L[i].adr, L[i].data, L[i + 1].adr);
    }
    printf("%05d %d -1\n", L[L.size() - 1].adr, L[L.size() - 1].data);
}

int main() {
    int h1, h2, n, adr;
    cin >> h1 >> h2 >> n;
    for (int i = 0; i < n; i++) {
        cin >> adr;
        cin >> Node[adr].data >> Node[adr].next;
        Node[adr].adr = adr;
    }
    generateList(l1, h1);
    generateList(l2, h2);
    if (l1.size() < l2.size()) {
        swap(l1, l2);
    }
    if (l1.size() >= l2.size() * 2) {
        int cnt = 0, pos = l2.size() - 1;
        for (auto it : l1) {
            ans.push_back(it);
            if (++cnt % 2 == 0 && pos >= 0) {
                ans.push_back(l2[pos--]);
            }
        }
    }
    printList(ans);
    return 0;
}
测试点 结果 耗时 内存
0 答案正确 2 ms 384KB
1 答案正确 2 ms 424KB
2 答案正确 2 ms 384KB
3 答案正确 3 ms 384KB
4 答案正确 152 ms 6388KB

7-3 Postfix Expression 答案正确 得分: 25 / 25

Given a syntax tree (binary), you are supposed to output the corresponding postfix expression, with parentheses reflecting the precedences of the operators.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (\le 20) which is the total number of nodes in the syntax tree. Then N lines follow, each gives the information of a node (the ii-th line corresponds to the ii-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-1. The figures 1 and 2 correspond to the samples 1 and 2, respectively.

infix1.JPG infix2.JPG
Figure 1 Figure 2

Output Specification:

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.

Sample Input 1:

8
* 8 7
a -1 -1
* 4 1
+ 2 5
b -1 -1
d -1 -1
- -1 6
c -1 -1

Sample Output 1:

(((a)(b)+)((c)(-(d))*)*)

Sample Input 2:

8
2.35 -1 -1
* 6 1
- -1 4
% 7 8
+ 2 3
a -1 -1
str -1 -1
871 -1 -1

Sample Output 2:

(((a)(2.35)*)(-((str)(871)%))+)
#include <iostream>
#include <string>
using namespace std;
const int maxn = 25;
bool vis[maxn];
struct node {
    string v;
    int lc, rc;
} Node[maxn];
int n;

bool isOperator(string str) {
	return str == "-" || str == "/" || str == "*" || str == "+";
}

void postOrder(int root) {
    if (root == -1) return;
    cout << "(";
    postOrder(Node[root].lc);
    if (isOperator(Node[root].v) && Node[root].lc == -1) {
        cout << Node[root].v;
        postOrder(Node[root].rc);
    } else {
        postOrder(Node[root].rc);
        cout << Node[root].v;
    }
    cout << ")";
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> Node[i].v >> Node[i].lc >> Node[i].rc;
        if (Node[i].lc != -1) vis[Node[i].lc] = true;
        if (Node[i].rc != -1) vis[Node[i].rc] = true;
    }
    int R;
    for (int i = 1; i <= n; i++) {
        if (!vis[i]) {
            R = i;
            break;
        }
    }
    postOrder(R);
    return 0;
}
测试点 结果 耗时 内存
0 答案正确 2 ms 384KB
1 答案正确 3 ms 356KB
2 答案正确 2 ms 384KB
3 答案正确 2 ms 512KB
4 答案正确 2 ms 384KB

7-4 Dijkstra Sequence 答案正确 得分: 30 / 30

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.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive integers NvN_v (103\le 10^3) and NeN_e (105\le 10^5), which are the total numbers of vertices and edges, respectively. Hence the vertices are numbered from 1 to NvN_v.

Then NeN_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\le 100) of the edge. It is guaranteed that the given graph is connected.

Finally the number of queries, KK, is given as a positive integer no larger than 100100, followed by KK lines of sequences, each contains a permutationof the NvN_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.

Output Specification:

For each of the KK sequences, print in a line Yes if it is a Dijkstra sequence, or No if not.

Sample Input:

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

Sample Output:

Yes
Yes
Yes
No
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
const int maxn = 1010;
const int INF = 0x7FFFFFFF;
int G[maxn][maxn], d[maxn], number[maxn];
bool vis[maxn];
int n, m, k;

bool Dijkstra(int s) {
    fill(vis, vis + maxn, false);
    fill(d, d + maxn, INF);
    d[s] = 0;
    for (int i = 1; i <= n; i++) {
        int u = -1, MIN = INF;
        for (int j = 1; j <= n; j++) {
            if (!vis[j] && d[j] < MIN) {
                u = j;
                MIN = d[j];
            }
        }
        if (u == -1 || d[u] != d[number[i]]) return false;
        u = number[i];
        vis[u] = true;
        for (int v = 1; v <= n; v++) {
            if (!vis[v] && G[u][v] != INF) {
                if (d[u] + G[u][v] < d[v]) {
                    d[v] = d[u] + G[u][v];
                }
            }
        }
    }
    return true;
}

int main() {
    scanf("%d%d", &n, &m);
    fill(G[0], G[0] + maxn * maxn, INF);
    int u, v, w;
    for (int i = 0; i < m; i++) {
        scanf("%d%d", &u, &v);
        scanf("%d", &G[u][v]);
        G[v][u] = G[u][v];
    }
    scanf("%d", &k);
    while (k--) {
        for (int i = 1; i <= n; i++) {
            cin >> number[i];
        }
        if (Dijkstra(number[1])) cout << "Yes\n";
        else cout << "No\n";
    }
    return 0;
}
测试点 结果 耗时 内存
0 答案正确 4 ms 4460KB
1 答案正确 5 ms 4320KB
2 答案正确 6 ms 4352KB
3 答案正确 463 ms 4384KB