华科计算机考研复试机试【零散大汇总】

 

【1】

屏幕的格式化输出:给出一幅图形,用程序实现。如下所示。

 

我的代码:

  1. #include<stdio.h>  
  2. int main(){  
  3.     char c;  
  4.     int sum,i,j;  
  5.     while(scanf("%c",&c)!=EOF){  
  6.         sum=c-'A'+1;  
  7.         for(i=0;i<sum;i++){  
  8.             for(j=0;j<2*(sum-1-i);j++){  
  9.                 printf(" ");//输出前边的空格;  
  10.             }  
  11.             for(j=0;j<=i;j++){  
  12.                 printf("%c",'A'+j);//输出前边一串字符串;  
  13.             }  
  14.             for(j=0;j<i;j++){  
  15.                 printf("  ");//输出中间的字符串;  
  16.             }  
  17.             for(j=i;j>=0;j--){  
  18.                 printf("%c",'A'+j);  
  19.             }  
  20.             printf("/n");  
  21.         }  
  22.         for(i=sum-2;i>=0;i--){  
  23.             for(j=0;j<2*(sum-1-i);j++){  
  24.                 printf(" ");//输出前边的空格;  
  25.             }  
  26.             for(j=0;j<=i;j++){  
  27.                 printf("%c",'A'+j);//输出前边一串字符串;  
  28.             }  
  29.             for(j=0;j<i;j++){  
  30.                 printf("  ");//输出中间的字符串;  
  31.             }  
  32.             for(j=i;j>=0;j--){  
  33.                 printf("%c",'A'+j);  
  34.             }  
  35.             printf("/n");  
  36.         }  
  37.         getchar();//接受回车符;  
  38.     }  
  39. }  

数据测试:

【2】已知二叉树的前序和中序序列,要求写出后序序列。如果不能构成一棵树,输出:“No Answer!”

参考代码:

 

  1. #include<stdio.h>  
  2. #include<string.h>  
  3. #include<stdlib.h>  
  4. typedef struct node{  
  5.         char data;  
  6.         struct node *lchild,*rchild;  
  7. }BTNode,*BTree;  
  8.   
  9. BTree createBTree(char *pre,int st1,int en1,char *in,int st2,int en2){  
  10.     BTree boot;  
  11.     boot=(BTree)malloc(sizeof(BTNode));  
  12.     int temp;  
  13.     temp=st2;  
  14.     int len_left;//左子树长度;  
  15.     //printf("***/n");  
  16.     if((en2-st2)!=(en1-st1)){  
  17.         printf("No Answer!/n");//不能构成一棵树;   
  18.         exit(0);  
  19.     }  
  20.     if(en1-st1<0||en2-st2<0){  
  21.         boot=NULL;  
  22.         return boot;  
  23.     }  
  24.     while(temp<=en2&&in[temp]!=pre[st1]){  
  25.           temp++;                      
  26.     }  
  27.     if(temp>en2){//注意:没有等号!  
  28.          printf("No Answer!/n");//不能构成一棵树;   
  29.          exit(0);          
  30.     }  
  31.     boot->data=pre[st1];  
  32.     len_left=temp-st2;  
  33.     boot->lchild=createBTree(pre,st1+1,st1+len_left,in,st2,temp-1);//构造左子树;   
  34.     boot->rchild=createBTree(pre,st1+len_left+1,en1,in,temp+1,en2);//构造左子树;   
  35.     return boot;  
  36. }  
  37.   
  38. void display(BTree T){//后序遍历二叉树;  
  39.     if(T!=NULL){  
  40.         display(T->lchild);  
  41.         display(T->rchild);  
  42.         printf("%c",T->data);  
  43.         free(T);//释放结点指针;  
  44.     }  
  45. }  
  46.   
  47. int main(){  
  48.     char pre[1000],in[1000];  
  49.     int len1,len2;  
  50.     BTree T;  
  51.     T=(BTree)malloc(sizeof(BTNode));  
  52.     while(scanf("%s%s",pre,in)!=EOF){  
  53.         len1=strlen(pre);  
  54.         len2=strlen(in);  
  55.         T=createBTree(pre,0,len1-1,in,0,len2-1);  
  56.         display(T);  
  57.         printf("/n");  
  58.     }  
  59.     //system("pause");  
  60. }  

 

数据测试:

 

【3】

 

1 生成一个长度为21的数组,依次存入1到21

2 建立一个长度为21的单向链表,将上述数组中的数字依次存入链表每个结点中

3 将上述链表变为单向封闭(循环)链表

4 从头结点开始数,将第17个结点删除,将它的下一个结点作为新的头结点

5 重复上述过程,直到该链表中只剩一个结点,显示该结点中存入的数字

参考代码:

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. typedef struct node{  
  4.     int data;  
  5.     struct node *next;  
  6. }LNode,*LinkList;  
  7.   
  8. LinkList createList(int a[],int len){//创建单链表;  
  9.     LinkList L,p,r;  
  10.     int i;  
  11.     if(len<=0){  
  12.         return NULL;  
  13.     }  
  14.     L=(LinkList)malloc(sizeof(LNode));  
  15.     L->data=a[0];  
  16.     p=L;  
  17.     for(i=1;i<len;i++){  
  18.         r=(LinkList)malloc(sizeof(LNode));  
  19.         r->data=a[i];  
  20.         p->next=r;  
  21.         p=r;  
  22.     }  
  23.     p->next=NULL;//表尾;  
  24.     return L;  
  25. }  
  26.   
  27. LinkList changeList(LinkList L){//把单链表变成单循环链表;  
  28.     LinkList p;  
  29.     p=L;  
  30.     while(p->next!=NULL){  
  31.         p=p->next;//p移至表尾;  
  32.     }  
  33.     p->next=L;  
  34.     return L;  
  35. }  
  36.   
  37. LinkList deleteLNode(LinkList L){//从链表中删除结点;  
  38.     LinkList p,r;  
  39.     int count=1;  
  40.     p=L;  
  41.     while(count<=16){  
  42.         r=p;//r指向前驱;  
  43.         p=p->next;//p移至第17个结点;  
  44.         count++;  
  45.     }  
  46.     r->next=p->next;  
  47.     free(p);  
  48.     return r->next;//新的头结点;  
  49. }  
  50.   
  51. void display(LinkList L){//输出单链表;  
  52.     if(L!=NULL){  
  53.         LinkList p;  
  54.         p=L;  
  55.         while(p!=NULL){  
  56.             printf("%d ",p->data);  
  57.             p=p->next;  
  58.         }  
  59.     }  
  60. }  
  61.   
  62. int main(){  
  63.     int a[21],i;  
  64.     LinkList L,p;  
  65.     for(i=0;i<21;i++){  
  66.         a[i]=i+1;  
  67.     }  
  68.     L=createList(a,21);  
  69.     L=changeList(L);  
  70.     //L=deleteLNode(L);  
  71.     while(L->next!=L){  
  72.         L=deleteLNode(L);  
  73.     }  
  74.     printf("链表中最后剩下的结点是:%d/n",L->data);  
  75.     //display(L);  
  76. }  

数据测试:

 

【4】对输入的5个数排序,输出的结果到文件里。

参考代码:

  1. #include<stdio.h>  
  2. #include<stdlib.h>  
  3. int cmp(const void *a,const void *b){  
  4.  return (*(int *)a)-(*(int *)b);  
  5. }  
  6.   
  7. int main(){  
  8.  int a[5],i;  
  9.  //char s[5];  
  10.  FILE *foutput;  
  11.  printf("请输入5个待排序的数:/n");  
  12.  for(i=0;i<5;i++){  
  13.   scanf("%d",&a[i]);  
  14.  }  
  15.  qsort(a,5,sizeof(int),cmp);  
  16.  if((foutput=fopen("0000华科复试机试题(4).txt","w"))==NULL){  
  17.   printf("File open error!/n");  
  18.  }  
  19.  for(i=0;i<5;i++){  
  20.   fputc(a[i]+'0',foutput);  
  21.  }  
  22.  fclose(foutput);  
  23. }  

数据测试:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值