模拟试题

3773考试网计算机等级考试模拟试题正文

2010年下半年NCRE二级C语言上机冲刺模拟一

来源:fjzsksw.com 2010-9-14 13:42:06

 

 

一、改错题

  含有错误的源程序:

  #include

  #include

  #define N 5

  struct student

  { char name[10];

  int score;

  } ―――――――――――①

  void sort(struct student stud[],int n)

  { int i,j;

  struct student p;

  for(i=1;i

  { p=stud[i];

  for(j=i-1;j>0 && p>stud[j];j--) ―――――――――――③

  stud[j+1]=stud[j];

  stud[j+1]=p;

  }

  }

  void main()

  { struct student stud[N]={"aaa",60,"bbb",90,"ccc",85,"ddd",65,"yyy",77};

  int i;

  sort(stud,N);

  printf("sorted data:\n");

  for(i=0;i

  }

  【知识点】结构体、排序

  【难点】 插入法排序

  【解析】本题是将未排序的数组元素采用插入排序方法进行排序,其思想是:从第2个元素开始,最初与第一个元素进行比较,由大到小排列在适当位置,成为排好序的数组元素一员。再依次将待排序的元素与前面已排好序的元素从后向前进行比较,如果大于该元素,则该元素向后移一位,直到待排序的元素小于已排好序的某一元素,则直接插入到该元素之后。直到待排序的元素全部完成为止。本程序的sort函数中i表示待排序的元素下标,j表示已排好序的元素下标。

  【答案】(1)标号①:} 改为 }; (2)标号②:for(i=1;i

  (3)标号③:for(j=i-1;j>0 && p>stud[j];j--) 改为 j>=0 && p.score>stud[j].score;j--)

  (4) 标号④:printf("%s\t%d\n",stud[i]); 改为printf("%s\t%d\n",stud[i].name,stud[i].score );

 

 

[1] [2] 下一页

触屏版 电脑版
3773考试网 琼ICP备12003406号-1