导航:首页 - 北大自考数据结构上机考试复习总结2

北大自考数据结构上机考试复习总结2
作者:深圳教育在线 来源:szedu.net 更新日期:2007-12-27
2.编一C程序,它能对输入的一串整数(不多于1000个,以-9999为结束标记)到数组a中,再对a的元素进行直接插入排序(从小到大排序),输出排序结果和所用关键字比较次数。(输入时,两个相邻的整数用空格隔开)。

  (注:程序的可执行文件名必须是 e2.exe,存于你的账号或其debug目录下。)

  #include

  #include

  void main()

  {

  int i,j, k1,k2,c[66],s,k,count=0,flag=0;

  int a[66];

  int b[66];

  printf(“请输入66个数到a中:\n”);

  for(i=0;i<66;i++)

  scanf(“%d”,&a[i]);

  printf(“请输入66个数到b中:\n”);

  for(i=0;i<66;i++)

  scanf(“%d”,&b[i]);

  for(i=0;i<11;i++){

  for(k=0;k<11;k++)

  {s=0;

  for(j=0;j<11&&i>=j;j++)

  k1=i*(i+1)/2+j;

  if(j>=k)

  k2=j*(j+1)/2+i;

  else

  continue;

  s+=a[k1]*b[k2];

  flag=1;

  }

  if(flag)

  {

  c[count++]=s;

  flag=0;

  }

  }

  for(i=0;i<66;i++)

  printf(“%d”,c[i]);

  }数据结构练习题3

  1. 编一C程序,它能根据输入的二叉树前序和中序序列来构造该二叉树,并能输出该二叉树的后序序列和该二叉树叶的结点的个数以及该二叉树高度。(输入次序是:表示前序序列的字符串、表示中序序列的字符串)。

  (注:程序的可执行文件名必须是 e1.exe,存于你的账号或其debug目录下。)

  #include

  #include

  #include

  void exit(int);

  #define MAX 100

  typedef struct node{

  char d;

  struct node *lchild,*rchild;

  }Tnode;

  void MKTree(char pre[],int pres,int pree,char in[],int is,int ie,Tnode **r)

  {

  int i;

  if(pres>pree||is>ie)

  *r=NULL;

  else{

  *r=malloc(sizeof(Tnode));

  for(i=is;i<=ie;i++)

  if(pre[pres]==in[i])

  {

  MKTree(pre,pres+1,pres+i-is,in,is,is+i-1,&(*r)->lchild);

  MKTree(pre,pres+i+is+1,pree,in,is+i+1,ie,&(*r)->rchild);

  break;

  }

  }

  }

  void postorder(Tnode *r)

  {

  if(r)

  {

  postorder(r->lchild);

  postorder(r->rchild);

  printf(“%c”,r->d);

  }

  }

  int num(Tnode *r)

  {

  if(r==NULL)

  return 0;

  else

  if(r->lchild==NULL&&r->rchild==NULL)

  return 1;

  else

  return num(r->lchild)+num(r->rchild);

  }

  int height(Tnode *r)

  {

  int h1,h2;

  if(r==NULL)

  return 0;

  else

  {

  h1=height(r->lchild);

  h2=height(r->rchild);

  return 1+(h1>h2)?h1:h2;

  }

  }

  void main()

  {

  Tnode *r;

  char pre[MAX],in[MAX];

  printf(“input preorder and inorder \n”);

  gets(pre);

  gets(in);

  MKTree(pre,0,strlen(pre)-1,in,0,strlen(in)-1,&r);

  printf(“The postorder is as follow:\n”);

  postorder(r);

  printf(“\n there are %d leaves in the tree\n”,num(r));

  printf(“h=%d\n”,height(r));

  }


报 名 此 课 程 / 咨 询 相 关 信 息
【预约登门】 【网上咨询】 【订座试听】 【现在报名】
课程名称
北大自考数据结构上机考试复习总结2
真实姓名
* 性 别
联系电话
* E-mail:
所在地区
咨询内容

      

相关文章:

Copyright© 2004-2010 www.szedu.net 深圳教育在线 版权所有
中国·深圳
粤ICP备06023013号