Beijing University of Posts and Telecommunications in 2019 on the subject of computer hospital re-examination machine

Topic Source: Beijing University of Posts and Telecommunications Institute of machine test Zhenti 2019 computer memories edition

2019. Academy .Problem A binary computer

Description Title
32-bit binary numbers X, its X + 1, X + 3 operations, and outputs. Be careful not to omit leading 0. (X <2 = 32 -3)

Input
of the first row, an integer T, represents the number of test data sets.
Then line T, the input 32 is a binary number

The output
of each test, two output lines, the first row X + 1, the second row X + 3.

Test sample
input

2
00000000000000000000000000000000
00000000000000000000000000000001

Export

00000000000000000000000000000001
00000000000000000000000000000011
00000000000000000000000000000010
00000000000000000000000000000100

Thinking
idea one: into the bit array using simulation and addition array
idea II: Use of type long long array 10 is converted to decimal to binary conversion is calculated

#include <iostream>
#include <string>
#include <cstring>
#include <cstdio>
using namespace std;

int c[32];
int add[32];

int main()
{
    int T;
    cin >> T;
    string str, temp;
    while( T-- )
    {
        cin >> str;
        temp = str;
        memset( c, 0, sizeof(c) );  //进位数组
        memset( add, 0, sizeof(add) );  // 加法
        add[31] = 1;
        for( int i = 31; i >= 0; i-- )
        {
            if( str[i]-'0' + c[i] + add[i] == 2 )
            {
                str[i] = '0';
                if( i>=1 )
                    c[i-1] = 1;
            }
            else if( str[i]-'0' + c[i] + add[i] == 3 )
            {
                str[i] = '1';
                if( i>=1 )
                    c[i-1] = 1;
            }
            else if( str[i]-'0' + c[i] + add[i] == 1 )
            {
                str[i] = '1';
            }
        }
        cout << str << endl;

        memset( c, 0, sizeof(c) );  //进位数组
        memset( add, 0, sizeof(add) );  // 加法
        add[31] = 1; add[30] = 1; str = temp;
        for( int i = 31; i >= 0; i-- )
        {
            if( str[i]-'0' + c[i] + add[i] == 2 )
            {
                str[i] = '0';
                if( i>=1 )
                    c[i-1] = 1;
            }
            else if( str[i]-'0' + c[i] + add[i] == 3 )
            {
                str[i] = '1';
                if( i>=1 )
                    c[i-1] = 1;
            }
            else if( str[i]-'0' + c[i] + add[i] == 1 )
            {
                str[i] = '1';
            }
        }
        cout << str << endl;
    }
    return 0;
}

2019. Computer Institute .Problem B. Binary Tree

Description Title
of the binary tree, to calculate the shortest path length of any two nodes.

Input
a first set of test data input line number of T
second input line n, m. representative of the number of nodes n, m represents the number of data sets to be queried
next n lines, two inputs per row, on behalf of the child node 1 to node n, if no child node enter -1 root node 1.
Subsequently m lines of two input numbers, representative of two nodes to query

Output
test data output line of each m, the length of the shortest path between two nodes representative of the query

Test sample
input

1
8 4
2 3
4 5
6 -1
-1 -1
-1 7
-1 -1
8 -1
\- 1 -1
1 6
4 6
4 5
8 1

Export

2
4
2
4

Ideas
template title, the right mouse button to copy.

Code

#include <iostream>
#include <stack>
#include <cstdio>

using namespace std;
const int maxn = 100;

int father[maxn];
stack<int> su, sv;

void init( int n )
{
    for( int i = 0; i <= n; i++ )
        father[i] = -1;
}

int main()
{
    freopen("in.txt", "r", stdin );
    int T, N, M;
    int u, v, fu, fv, lc, rc;
    int path;
    cin >> T;
    while( T-- )
    {
        cin >> N >> M;
        init(N);
        for( int i = 1; i <= N; i++ )
        {
            cin >> lc >> rc;
            if( lc != -1 )
                father[lc] = i;
            if( rc != -1 )
                father[rc] = i;
        }

        while( M-- )
        {
            path = 0;
            cin >> u >> v;
            while( !su.empty() )
                su.pop();
            while( !sv.empty() )
                sv.pop();
            su.push(u);
            sv.push(v);
            while( father[u] != -1 )
            {
                su.push( father[u] );
                u = father[u];
                path++;
            }
            while( father[v] != -1 )
            {
                sv.push( father[v] );
                v = father[v];
                path++;
            }

            while( !su.empty() && !sv.empty() )
            {
                fu = su.top();
                fv = sv.top();
                if( fu == fv )
                {
                    su.pop();
                    sv.pop();
                    path-=2;
                }
                else
                    break;
            }
            cout << path+2 << endl;
        }
    }
    return 0;
}

2019. Computer Institute .Problem C. Shortest Path

Title Description
day and night from city to city n 1, a number of dark lines will turn off, respectively, to find the shortest path urban city n 1 in day and night.
Note: This topic does not guarantee that there will be no multiple edges.

Input
a first set of row data number T
second input line n, m, k. N represents the number of cities, m represents the number of paths, k representative of the number of paths is closed at night
Subsequently m rows, each row input three numbers x, y, val, x represents the distance communication between cities and urban val y is
the number of the last line k, representative Close night line number (line number refers to a 1 ~ m)
is not guaranteed without heavy side, which is the examination: thanks supplement and later reissued announcement, so this question a few people out there for this reason

Output
each two data output lines, the shortest path representing day and night, urban 1 to n

Sample input

1
4 4 1
1 2 1
2 3 1
3 4 1
1 4 1
4

Sample Output

1
3

Thinking
there is this question multiple edges, hey, bye! ! !

2019. Computer Institute .Problem D. *****

Ok. This question is. . . No problem. . . . Ok. . . I no rain melon. . . . . .

Published 39 original articles · won praise 1 · views 1130

Guess you like

Origin blog.csdn.net/Redemption1997/article/details/103951392