北邮2012年复试上机题目

前言

每次让我看到北邮的上机题目不管当时多忙我都必须把它们ac,原因是当年北邮复试,上机题目竟然一道也没有做出来,旁边一同学还抱怨和我坐一起连个讨论的人都没有!唉,往事不堪回首,这里记录一下2012年北邮上机的题目吧,每年复试前才会放出上一年的上机题目

二进制数

题目

题目描述:
  大家都知道,数据在计算机里中存储是以二进制的形式存储的。
  有一天,小明学了C语言之后,他想知道一个类型为unsigned int 类型的数字,存储在计算机中的二进制串是什么样子的。
  你能帮帮小明吗?并且,小明不想要二进制串中前面的没有意义的0串,即要去掉前导0。
输入:
第一行,一个数字T(T<=1000),表示下面要求的数字的个数。
接下来有T行,每行有一个数字n(0<=n<=10^8),表示要求的二进制串。
输出:
输出共T行。每行输出求得的二进制串。
样例输入:
5
23
535
2624
56275
989835
样例输出:
10111
1000010111
101001000000
1101101111010011
11110001101010001011

ac代码

没什么可说的,简单的机制转换,连大数除法都没考察!

#include 
#include 
#include 
 
struct stack
{
    int top;
    int data[100];
};
 
void convert_to_binary(struct stack *s, unsigned long int d)
{
    s->top = 0;
 
    while (d) {
        s->data[s->top ++] = d % 2;
        d /= 2;
    }
 
    while (s->top) {
        printf("%d", s->data[-- s->top]);
    }
    printf("\n");
}
 
int main()
{
    int i, n;
    unsigned long int d;
    struct stack *s = (struct stack*)malloc(sizeof(struct stack));
 
    while (scanf("%d", &n) != EOF) {
        for (i = 0; i < n; i ++) {
            scanf("%ld", &d);
            if (d != 0) {
                convert_to_binary(s, d);
            }else {
                printf("0\n");
            }
        }
    }
 
    return 0;
}
/**************************************************************
    Problem: 1473
    User: wangzhengyi
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:904 kb
****************************************************************/

二叉排序树

题目

题目描述:
        二叉排序树,也称为二叉查找树。可以是一颗空树,也可以是一颗具有如下特性的非空二叉树:

        1. 若左子树非空,则左子树上所有节点关键字值均不大于根节点的关键字值;
        2. 若右子树非空,则右子树上所有节点关键字值均不小于根节点的关键字值;
        3. 左、右子树本身也是一颗二叉排序树。

  现在给你N个关键字值各不相同的节点,要求你按顺序插入一个初始为空树的二叉排序树中,每次插入后成功后,求相应的父亲节点的关键字值,如果没有父亲节点,则输出-1。
输入:
输入包含多组测试数据,每组测试数据两行。
第一行,一个数字N(N<=100),表示待插入的节点数。
第二行,N个互不相同的正整数,表示要顺序插入节点的关键字值,这些值不超过10^8。
输出:
输出共N行,每次插入节点后,该节点对应的父亲节点的关键字值。
样例输入:
5
2 5 1 3 4
样例输出:
-1
2
2
5
3

ac代码

没什么思路,最简单的构建二叉排序树而已
#include 
#include 
#include 
 
struct btree
{
    struct btree *lchild, *rchild;
    unsigned long int data;
};
 
struct btree* create_btree(struct btree *t, unsigned long int d, unsigned long int parent);
 
int main()
{
    int i, n;
    unsigned long int d;
    struct btree *t;
 
    while (scanf("%d", &n) != EOF) {
        t = NULL;
        for (i = 0; i < n; i ++) {
            scanf("%ld", &d);
            t = create_btree(t, d, -1);
        }
    }
 
    return 0;
}
 
struct btree* create_btree(struct btree *t, unsigned long int d, unsigned long int parent)
{
    if (t == NULL) {
        t = (struct btree *)malloc(sizeof(struct btree));
        t->data = d;
        t->lchild = NULL;
        t->rchild = NULL;
        printf("%ld\n", parent);        
    }else if(t->data > d) {
        t->lchild = create_btree(t->lchild, d, t->data);
    }else if(t->data < d) {
        t->rchild = create_btree(t->rchild, d, t->data);
    }else {
        exit(EXIT_FAILURE);
    }
 
    return t;
}
 
/**************************************************************
    Problem: 1467
    User: wangzhengyi
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:904 kb
****************************************************************/

矩阵幂

题目

题目描述:
给定一个n*n的矩阵,求该矩阵的k次幂,即P^k。
输入:
输入包含多组测试数据。
数据的第一行为一个整数T(0


ac代码

这个也是挺简单的,就是个矩阵乘法,三个for循环即可
#include 
#include 
#include 
 
#define LEN 15
 
int a[LEN][LEN], b[LEN][LEN], c[LEN][LEN];
 
void multiplay_matrix();
 
 
int main()
{
    int t, n, k, i, j, d;
 
    scanf("%d", &t);
    while (t --) {
        // 接收矩阵
        scanf("%d %d", &n, &k);
        for (i = 0; i < n; i ++) {
            for (j = 0; j < n; j ++) {
                scanf("%d", &d);
                a[i][j] = d;
                b[i][j] = d;
                c[i][j] = d;
            }
        }
 
        // 矩阵的幂
        if (k != 1) {
            multiplay_matrix(k, n);
        }
 
        for (i = 0; i < n; i ++) {
            for (j = 0; j < n; j ++) {
                if (j == n - 1) {
                    printf("%d\n", c[i][j]);
                }else {
                    printf("%d ", c[i][j]);
                }
            }
        }
    }
 
    return 0;
}
 
void multiplay_matrix(int k, int n)
{
    int i, j, h, data;
    k --;
    while (k --) {
        for (i = 0; i < n; i ++) {
            for (j = 0; j < n; j ++) {
                for (h = data = 0; h < n; h ++) {
                    data += b[i][h] * a[h][j];
                }
                c[i][j] = data;
            }
        }
        for (i = 0; i < n; i ++) {
            for (j = 0; j < n; j ++) {
                b[i][j] = c[i][j];
            }
        }
    }
}
/**************************************************************
    Problem: 1474
    User: wangzhengyi
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:912 kb
****************************************************************/

IP数据包解析

题目


头部长度单位为4字节。
  你的任务是,简要分析输入数据中的若干个TCP数据段的头部。 详细要求请见输入输出部分的说明。
输入:
第一行为一个整数T,代表测试数据的组数。
以下有T行,每行都是一个TCP数据包的头部分,字节用16进制表示,以空格隔开。数据保证字节之间仅有一个空格,且行首行尾没有多余的空白字符。
保证输入数据都是合法的。
输出:
对于每个TCP数据包,输出如下信息:
Case #x,x是当前测试数据的序号,从1开始。
Total length = L bytes,L是整个IP数据包的长度,单位是1字节。
Source = xxx.xxx.xxx.xxx,用点分十进制输出源IP地址。输入数据中不存在IPV6数据分组。
Destination = xxx.xxx.xxx.xxx,用点分十进制输出源IP地址。输入数据中不存在IPV6数据分组。
Source Port = sp,sp是源端口号。
Destination Port = dp,dp是目标端口号。
对于每个TCP数据包,最后输出一个多余的空白行。
具体格式参见样例。
请注意,输出的信息中,所有的空格、大小写、点符号、换行均要与样例格式保持一致,并且不要在任何数字前输出多余的前导0,也不要输出任何不必要的空白字符。
样例输入:
2
45 00 00 34 7a 67 40 00 40 06 63 5a 0a cd 0a f4 7d 38 ca 09 cd f6 00 50 b4 d7 ae 1c 9b cf f2 40 80 10 ff 3d fd d0 00 00 01 01 08 0a 32 53 7d fb 5e 49 4e c8
45 00 00 c6 56 5a 40 00 34 06 e0 45 cb d0 2e 01 0a cd 0a f4 00 50 ce 61 e1 e9 b9 ee 47 c7 37 34 80 18 00 b5 81 8f 00 00 01 01 08 0a 88 24 fa c6 32 63 cd 8d
样例输出:
Case #1
Total length = 52 bytes
Source = 10.205.10.244
Destination = 125.56.202.9
Source Port = 52726
Destination Port = 80

Case #2
Total length = 198 bytes
Source = 203.208.46.1
Destination = 10.205.10.244
Source Port = 80
Destination Port = 52833


ac代码

注意取源端口号和目的端口号时需要注意ip头部长度的判断,IHL,其它就没神马难度了

#include 
#include 
#include 
 
#define LEN 1000
 
int change_tint(char *str, int begin, int num)
{
    int i;
    char *temp = (char *)malloc(sizeof(char) * (num + 1));
 
    for(i = 0; i < num; i ++) {
        temp[i] = str[begin + i];
    }
    temp[i] = '\0';
 
    return strtol(temp, NULL, 16);
}
 
void ip_field(char *str, int begin, int num)
{
    int i, flag, ip;
    for (i = 0, flag = 1; i < num; i += 2) {
        ip = change_tint(str, begin + i, 2);
        printf("%d", ip);
        if (flag <= 3) {
            printf(".");
            flag ++;
        }
    }
    printf("\n");
}
 
 
int main()
{
    int index, i, j, n, length, ihl;
    char ipstr[LEN], temp[LEN];
 
    while (scanf("%d\n", &n) != EOF) {
        if (n != 0) {
            for (index = 1; index <= n; index ++) {
                memset(ipstr, 0, sizeof(ipstr));
                memset(temp, 0, sizeof(temp));  
                gets(temp);
                // 去除空格
                for (i = j = 0, length = strlen(temp); i < length; i ++) {
                    if (temp[i] != ' ') {
                        ipstr[j ++] = temp[i];
                    }
                }
                ipstr[j] = '\0';
 
                // 当前测试数据的序号
                printf("Case #%d\n", index);
 
                // 整个ip数据包的长度
                length = change_tint(ipstr, 4, 4);
                printf("Total length = %d bytes\n", length);
 
                // 源ip地址和目的ip地址
                printf("Source = ");
                ip_field(ipstr, 24, 8);
                printf("Destination = ");
                ip_field(ipstr, 32, 8);
 
                // 源端口号和目的端口号
                ihl = change_tint(ipstr, 1, 1) * 4 * 2;
                printf("Source Port = %d\n", change_tint(ipstr, ihl, 4));
                printf("Destination Port = %d\n", change_tint(ipstr, ihl + 4, 4));
                printf("\n");
            }
        }
    }
    return 0;
}
 
/**************************************************************
    Problem: 1475
    User: wangzhengyi
    Language: C
    Result: Accepted
    Time:10 ms
    Memory:908 kb
****************************************************************/

后记

2011年北邮复试一道上机题都不会做的我,现在两年后确实秒杀北邮的这些复试上机题,总共加调试差不多花了一个半小时,不过2012年北邮计算机院复试题目确实没神马难度!

你可能感兴趣的:(C/C++,ACM)