北邮计算机考研复试题的C语言解答精选
二进制数 题目描述:
#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("0n"); } } } return 0; } /************************************************************** 二叉排序树 题目描述:
#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 parent) { if (t == NULL) { t = (struct btree *)malloc(sizeof(struct btree)); t->data = d; t->lchild = NULL; t->rchild = NULL; printf("%ldn",parent); }else if(t->data > d) { t->lchild = create_btree(t->lchild,t->data); }else if(t->data < d) { t->rchild = create_btree(t->rchild,t->data); }else { exit(EXIT_FAILURE); } return t; }
题目描述: ac代码
#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("%dn",c[i][j]); }else { printf("%d ",c[i][j]); } } } } return 0; } void multiplay_matrix(int k,int n) { int i,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]; } } } } /**************************************************************
头部长度单位为4字节。 ac代码 #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] = ' '; return strtol(temp,NULL,16); } void ip_field(char *str,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,length,ihl; char ipstr[LEN],temp[LEN]; while (scanf("%dn",&n) != EOF) { if (n != 0) { for (index = 1; index <= n; index ++) { memset(ipstr,sizeof(ipstr)); memset(temp,sizeof(temp)); gets(temp); // 去除空格 for (i = j = 0,length = strlen(temp); i < length; i ++) { if (temp[i] != ' ') { ipstr[j ++] = temp[i]; } } ipstr[j] = ' '; // 当前测试数据的序号 printf("Case #%dn",index); // 整个ip数据包的长度 length = change_tint(ipstr,4,4); printf("Total length = %d bytesn",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 = %dn",change_tint(ipstr,ihl,4)); printf("Destination Port = %dn",ihl + 4,4)); printf("n"); } } } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |