北郵計(jì)算機(jī)考研復(fù)試題的C語(yǔ)言解答精選
二進(jìn)制數(shù)
題目
題目描述:
大家都知道,數(shù)據(jù)在計(jì)算機(jī)里中存儲(chǔ)是以二進(jìn)制的形式存儲(chǔ)的。
有一天,小明學(xué)了C語(yǔ)言之后,他想知道一個(gè)類(lèi)型為unsigned int 類(lèi)型的數(shù)字,存儲(chǔ)在計(jì)算機(jī)中的二進(jìn)制串是什么樣子的。
你能幫幫小明嗎?并且,小明不想要二進(jìn)制串中前面的沒(méi)有意義的0串,即要去掉前導(dǎo)0。
輸入:
第一行,一個(gè)數(shù)字T(T<=1000),表示下面要求的數(shù)字的個(gè)數(shù)。
接下來(lái)有T行,每行有一個(gè)數(shù)字n(0<=n<=10^8),表示要求的二進(jìn)制串。
輸出:
輸出共T行。每行輸出求得的二進(jìn)制串。
樣例輸入:
5
23
535
2624
56275
989835
樣例輸出:
10111
1000010111
101001000000
1101101111010011
11110001101010001011
ac代碼
沒(méi)什么可說(shuō)的,簡(jiǎn)單的機(jī)制轉(zhuǎn)換,連大數(shù)除法都沒(méi)考察!
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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
****************************************************************/
二叉排序樹(shù)
題目
題目描述:
二叉排序樹(shù),也稱(chēng)為二叉查找樹(shù)??梢允且活w空樹(shù),也可以是一顆具有如下特性的非空二叉樹(shù):
1. 若左子樹(shù)非空,則左子樹(shù)上所有節(jié)點(diǎn)關(guān)鍵字值均不大于根節(jié)點(diǎn)的關(guān)鍵字值;
2. 若右子樹(shù)非空,則右子樹(shù)上所有節(jié)點(diǎn)關(guān)鍵字值均不小于根節(jié)點(diǎn)的關(guān)鍵字值;
3. 左、右子樹(shù)本身也是一顆二叉排序樹(shù)。
現(xiàn)在給你N個(gè)關(guān)鍵字值各不相同的節(jié)點(diǎn),要求你按順序插入一個(gè)初始為空樹(shù)的二叉排序樹(shù)中,每次插入后成功后,求相應(yīng)的父親節(jié)點(diǎn)的關(guān)鍵字值,如果沒(méi)有父親節(jié)點(diǎn),則輸出-1。
輸入:
輸入包含多組測(cè)試數(shù)據(jù),每組測(cè)試數(shù)據(jù)兩行。
第一行,一個(gè)數(shù)字N(N<=100),表示待插入的節(jié)點(diǎn)數(shù)。
第二行,N個(gè)互不相同的正整數(shù),表示要順序插入節(jié)點(diǎn)的關(guān)鍵字值,這些值不超過(guò)10^8。
輸出:
輸出共N行,每次插入節(jié)點(diǎn)后,該節(jié)點(diǎn)對(duì)應(yīng)的父親節(jié)點(diǎn)的關(guān)鍵字值。
樣例輸入:
5
2 5 1 3 4
樣例輸出:
-1
2
2
5
3
ac代碼
沒(méi)什么思路,最簡(jiǎn)單的構(gòu)建二叉排序樹(shù)而已
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
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
****************************************************************/
矩陣冪
題目
題目描述:
給定一個(gè)n*n的矩陣,求該矩陣的k次冪,即P^k。
輸入:
輸入包含多組測(cè)試數(shù)據(jù)。
數(shù)據(jù)的第一行為一個(gè)整數(shù)T(0<T<=10),表示要求矩陣的個(gè)數(shù)。
接下來(lái)有T組測(cè)試數(shù)據(jù),每組數(shù)據(jù)格式如下:
第一行:兩個(gè)整數(shù)n(2<=n<=10)、k(1<=k<=5),兩個(gè)數(shù)字之間用一個(gè)空格隔開(kāi),含義如上所示。
接下來(lái)有n行,每行n個(gè)正整數(shù),其中,第i行第j個(gè)整數(shù)表示矩陣中第i行第j列的矩陣元素Pij且(0<=Pij<=10)。另外,數(shù)據(jù)保證最后結(jié)果不會(huì)超過(guò)10^8。
輸出:
對(duì)于每組測(cè)試數(shù)據(jù),輸出其結(jié)果。格式為:
n行n列個(gè)整數(shù),每行數(shù)之間用空格隔開(kāi),注意,每行最后一個(gè)數(shù)后面不應(yīng)該有多余的空格。
樣例輸入:
3
2 2
9 8
9 3
3 3
4 8 4
9 3 0
3 5 7
5 2
4 0 3 0 1
0 0 5 8 5
8 9 8 5 3
9 6 1 7 8
7 2 5 7 3
樣例輸出:
153 96
108 81
1216 1248 708
1089 927 504
1161 1151 739
47 29 41 22 16
147 103 73 116 94
162 108 153 168 126
163 67 112 158 122
152 93 93 111 97
ac代碼
這個(gè)也是挺簡(jiǎn)單的,就是個(gè)矩陣乘法,三個(gè)for循環(huán)即可
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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數(shù)據(jù)包解析
題目

頭部長(zhǎng)度單位為4字節(jié)。
你的任務(wù)是,簡(jiǎn)要分析輸入數(shù)據(jù)中的若干個(gè)TCP數(shù)據(jù)段的頭部。 詳細(xì)要求請(qǐng)見(jiàn)輸入輸出部分的說(shuō)明。
輸入:
第一行為一個(gè)整數(shù)T,代表測(cè)試數(shù)據(jù)的組數(shù)。
以下有T行,每行都是一個(gè)TCP數(shù)據(jù)包的頭部分,字節(jié)用16進(jìn)制表示,以空格隔開(kāi)。數(shù)據(jù)保證字節(jié)之間僅有一個(gè)空格,且行首行尾沒(méi)有多余的空白字符。
保證輸入數(shù)據(jù)都是合法的。
輸出:
對(duì)于每個(gè)TCP數(shù)據(jù)包,輸出如下信息:
Case #x,x是當(dāng)前測(cè)試數(shù)據(jù)的序號(hào),從1開(kāi)始。
Total length = L bytes,L是整個(gè)IP數(shù)據(jù)包的長(zhǎng)度,單位是1字節(jié)。
Source = xxx.xxx.xxx.xxx,用點(diǎn)分十進(jìn)制輸出源IP地址。輸入數(shù)據(jù)中不存在IPV6數(shù)據(jù)分組。
Destination = xxx.xxx.xxx.xxx,用點(diǎn)分十進(jìn)制輸出源IP地址。輸入數(shù)據(jù)中不存在IPV6數(shù)據(jù)分組。
Source Port = sp,sp是源端口號(hào)。
Destination Port = dp,dp是目標(biāo)端口號(hào)。
對(duì)于每個(gè)TCP數(shù)據(jù)包,最后輸出一個(gè)多余的空白行。
具體格式參見(jiàn)樣例。
請(qǐng)注意,輸出的信息中,所有的空格、大小寫(xiě)、點(diǎn)符號(hào)、換行均要與樣例格式保持一致,并且不要在任何數(shù)字前輸出多余的前導(dǎo)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代碼
注意取源端口號(hào)和目的端口號(hào)時(shí)需要注意ip頭部長(zhǎng)度的判斷,IHL,其它就沒(méi)神馬難度了
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#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';
// 當(dāng)前測(cè)試數(shù)據(jù)的序號(hào)
printf("Case #%d\n", index);
// 整個(gè)ip數(shù)據(jù)包的長(zhǎng)度
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);
// 源端口號(hào)和目的端口號(hào)
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
****************************************************************/
- C語(yǔ)言 坐標(biāo)移動(dòng)詳解及實(shí)例代碼
- c語(yǔ)言計(jì)算三角形面積代碼
- C語(yǔ)言中字符的輸入輸出以及計(jì)算字符個(gè)數(shù)的方法詳解
- C語(yǔ)言求冪計(jì)算的高效解法
- C語(yǔ)言實(shí)現(xiàn)計(jì)算樹(shù)的深度的方法
- C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 安裝OpenMPI來(lái)配合C語(yǔ)言程序進(jìn)行并行計(jì)算
- C語(yǔ)言科學(xué)計(jì)算入門(mén)之矩陣乘法的相關(guān)計(jì)算
- C語(yǔ)言簡(jiǎn)單實(shí)現(xiàn)計(jì)算字符個(gè)數(shù)的方法
- C語(yǔ)言中計(jì)算二叉樹(shù)的寬度的兩種方式
- C語(yǔ)言實(shí)現(xiàn)直角坐標(biāo)轉(zhuǎn)換為極坐標(biāo)的方法
相關(guān)文章
形參出現(xiàn)在函數(shù)定義中,在整個(gè)函數(shù)體內(nèi)都可以使用, 離開(kāi)該函數(shù)則不能使用。實(shí)參出現(xiàn)在主調(diào)函數(shù)中,進(jìn)入被調(diào)函數(shù)后,實(shí)參變量也不能使用,形參和實(shí)參的功能是作數(shù)據(jù)傳送。發(fā)生函數(shù)調(diào)用時(shí), 主調(diào)函數(shù)把實(shí)參的值傳送給被調(diào)函數(shù)的形參從而實(shí)現(xiàn)主調(diào)函數(shù)向被調(diào)函數(shù)的數(shù)據(jù)傳送2023-02-02
C語(yǔ)言實(shí)現(xiàn)數(shù)學(xué)表達(dá)式運(yùn)算
這篇文章主要為大家詳細(xì)介紹了c語(yǔ)言實(shí)現(xiàn)數(shù)學(xué)表達(dá)式運(yùn)算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-11-11
C/C++ 原生API實(shí)現(xiàn)線(xiàn)程池的方法
線(xiàn)程池,簡(jiǎn)單來(lái)說(shuō)就是有一堆已經(jīng)創(chuàng)建好的線(xiàn)程,接下來(lái)通過(guò)本文給大家介紹C/C++ 原生API實(shí)現(xiàn)線(xiàn)程池的方法,感興趣的朋友跟隨小編一起看看吧2021-11-11
C語(yǔ)言動(dòng)態(tài)內(nèi)存分配圖文講解
給數(shù)組分配多大的空間?你是否和初學(xué)C時(shí)的我一樣,有過(guò)這樣的疑問(wèn)。這一期就來(lái)聊一聊動(dòng)態(tài)內(nèi)存的分配,讀完這篇文章,你可能對(duì)內(nèi)存的分配有一個(gè)更好的理解2023-01-01
C語(yǔ)言簡(jiǎn)明講解歸并排序的應(yīng)用
這篇文章主要介紹了 c語(yǔ)言排序之歸并排序,歸并就是把兩個(gè)或多個(gè)序列合并,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-05-05
C++實(shí)現(xiàn)編寫(xiě)二維碼的示例代碼
這篇文章主要為大家詳細(xì)介紹如何基于C++實(shí)現(xiàn)編寫(xiě)二維碼的功能,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06

