2013年计算机二级C语言上机题库三_第3页

考试站(www.examzz.com)   【考试站:中国教育考试第一门户】   2013年2月26日
 编程题
  请编写函数void fun(int x,int pp[],int *n),它的功能是:求出能整除x且不是偶数的各整数,并按从小到大的顺序放在 pp所指的数组中,这些除数的个数通过形参 n返回.
  例如,若x中的值为 30,则有 4个数符合要求,它们是 1,3,5,15.
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:#include
  #include
  void fun(int x, int pp[], int *n)
  {
  }
  main()
  {
  int x, aa[1000], n, i;
  FILE *out;
  printf("\nPlease enter an integer number:\n");
  scanf("%d",&x);
  fun(x,aa,&n);
  for(i=0;i  printf("%d ", aa[i]);
  printf("\n");
  fun(730, aa, &n);
  out = fopen("out.dat", "w");
  for (i = 0; i < n; i++)
  fprintf(out, "%d\n", aa[i]);
  fclose(out);
  }
  答案是:
  void fun(int x,int pp[],int *n)
  {
  int i=1,j=0,k=0,*t=pp;
  for(i=0;i<=x;i++)
  if(i%2!=0)
  {
  t[j]=i;
  j++;
  }
  for(i=0;i  if(x%t[i]==0)
  {
  pp[k]=t[i];
  k++;
  }
  *n=k;
  }
首页 1 2 3 尾页

相关文章