2019年北邮计算机院复试上机题目

题目来源:北邮2019计算机院机试真题回忆版

2019.计算机院.Problem A 二进制

题目描述
32位二进制数X,对其进行X+1,X+3 操作,并输出。注意不能忽略前导0。(x<=232 -3)

输入
第一行,一个整数T,代表测试数据组数。
接着T行,输入32为二进制数

输出
对每组测试数据,输出两行,第一行为X+1,第二行为X+3.

测试样例
输入

2
00000000000000000000000000000000
00000000000000000000000000000001

输出

00000000000000000000000000000001
00000000000000000000000000000011
00000000000000000000000000000010
00000000000000000000000000000100

思路
思路一: 使用进位数组和加法数组模拟计算
思路二: 使用long long类型将数组转化为10进制计算 在转化为2进制

#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.计算机院.Problem B. 二叉树

题目描述
对二叉树,计算任意两个结点的最短路径长度。

输入
第一行输入测试数据组数T
第二行输入n,m 。n代表结点的个数,m代表要查询的数据组数
接下来n行,每行输入两个数,代表1~n结点的孩子结点,如果没有孩子结点则输入-1.根节点为1.
接下来m行,每行输入两个数,代表要查询的两个结点

输出
每组测试数据输出m行,代表查询的两个结点之间的最短路径长度

测试样例
输入

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

输出

2
4
2
4

思路
模板题,鼠标右键复制即可。

代码

#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.计算机院.Problem C.最短路径

题目描述
在白天和黑夜要从城市1到城市n,黑夜会关掉若干条线路,分别寻找城市1到城市n的在白天和黑夜的最短路径。
注意: 此题并不保证不会存在多重边。

输入
第一行数据组数T
第二行输入n,m,k. n代表城市数,m代表路径数,k代表夜间关闭的路径数
接下来m行,每行输入三个数x,y,val,代表城市x和城市y之间连通的距离为val
最后一行k个数,代表晚上关闭的线路序号(线路序号指的是1~m)
感谢补充:不保证无重边,这是考试时,后来补发的公告,所以这题a出的人很少也有这个原因

输出
每组数据输出两行,分别代表白天和黑夜,城市1到n的最短路径

样例输入

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

样例输出

1
3

思路
此题存在多重边,嘿嘿,拜拜!!!

2019.计算机院.Problem D.*****

嗯。这道题。。。没题目。。。。嗯。。。雨我无瓜。。。。。。

发布了39 篇原创文章 · 获赞 1 · 访问量 1130

猜你喜欢

转载自blog.csdn.net/Redemption1997/article/details/103951392