当前所在位置:珠峰网资料 >> 计算机 >> 计算机等级考试 >> 正文
2015年计算机二级C语言上机操作题及答案(23)
发布时间:2011/7/8 11:34:02 来源:城市学习网 编辑:ziteng
  第23套
  填空题
  在主函数中从键盘输入若干个数放入数组X中,用0结束输入但不计入数组。下列给定程序中,函数FUN的功能是:输出数组元素中小于平均值的元素。
  例如:数组中元素的值依次为1,2,2,12,5,15,则程序的运行结果为1,2,2,5。
  注意:部分源程序给出如下
  请勿改动主函数main和其他函数中的任何内容,仅在横线上填入所编写的若干表达式或语句。
  试题程序:#include <conio.h>
  #include <stdio.h>
  void fun(___1___, int  n)
  {
  double  sum = 0.0;
  double  average = 0.0;
  int  i = 0;
  for (i=0; i<n; i++)
  ___2___;
  average = ___3___;
  for (i=0; i<n; i++)
  if (x[i] < average)
  {
  if (i%5 == 0)
  printf("\n");
  printf("%d, ", x[i]);
  }
  }
  main()
  {
  int  x[1000];
  int  i = 0;
  printf("\nPlease enter some data(end with 0):");
  do
  {
  scanf("%d", &x[i]);
  } while (x[i++] != 0);
  fun(x, i-1);
  }
  第1处填空:int x[]或int *x
  第2处填空:sum+=x[i]或sum=sum+x[i]
  第3处填空:sum/n [NextPage]    改错题
  下列给定程序中,函数FUN的功能是:按以下递归公式求函数值。
  例如,当给n输入 5时, 函数值为18,当给 n输入 3时, 函数值为14.
  请改正程序中的错误,使它能得出正确结果。
  注意:不要改动MAIN函数,不得增行或删行,也不要更改程序的结构!
  试题程序:#include <stdio.h>
  /********found********/
  int fun(n)
  {
  int  c;
  /********found********/
  if (n = 1)
  c = 10;
  else
  c = fun(n-1)+2;
  return (c);
  }
  main()
  {
  int  n;
  printf("Enter n: ");
  scanf("%d", &n);
  printf("The result:%d\n\n", fun(n));
  }
  第1处:int fun(n)应改为int fun(int n)
  第2处:if (n=1)应改为if(n==1)
[NextPage]   编程题
  编写函数fun,它的功能是:判断字符串是否为回文,若是,则函数返回1,主函数输出 YES,否则返回0,主函数中输出 NO。回文是指顺读和倒读都一样的字符串,
  例如,字符串LEVEL是回文,而字符串123312就不是回文。
  注意:部分源程序给出如下。
  请勿改动主函数main和其他函数中的任何内容,仅在函数fun的花括号中填入所编写的若干语句。
  试题程序:
  #include <stdio.h>
  #define N 80
  int fun(char *str)
  {
  }
  main()
  {
  char s[N] ;
  FILE *out;
  char *test[] = {"1234321", "123421", "123321", "abcdCBA"};
  int i;
  printf("Enter a string: ") ;
  gets(s) ;
  printf("\n\n") ;
  puts(s) ;
  if(fun(s))
  printf("  YES\n") ;
  else
  printf("  NO\n") ;
  out=fopen("out.dat", "w");
  for (i = 0; i < 4; i++)
  if (fun(test[i]))
  fprintf(out, "YES\n");
  else
  fprintf(out, "NO\n");
  fclose(out);
  }
  答案是:
  int fun(char *str)
  {
  int i,n=0,fg=1;
  char *p=str;
  while(*p)
  {
  n++;
  p++;
  }
  for(i=0;i<n/2;i++)
  if(str[i]==str[n-1-i])
  else
  {
  fg=0;
  break;
  }
  return fg;
  }
广告合作:400-664-0084 全国热线:400-664-0084
Copyright 2010 - 2017 www.my8848.com 珠峰网 粤ICP备15066211号
珠峰网 版权所有 All Rights Reserved