已知数据文件in.dat中存放有300个四位数,并已调用读函数readDat()把这些数存入数组a中,请编写函数jsValue(),其功能是:求出千位数上的数加个位数上的数等于百位数上的数加十位数上的数的个数cnt,再把所有满足此条件的四位数依次存入数组b

admin2013-12-06  18

问题 已知数据文件in.dat中存放有300个四位数,并已调用读函数readDat()把这些数存入数组a中,请编写函数jsValue(),其功能是:求出千位数上的数加个位数上的数等于百位数上的数加十位数上的数的个数cnt,再把所有满足此条件的四位数依次存入数组b中,然后对数组b巾的四位数按从小到大的顺序进行排序。最后通过main()函数调用写函数writeDat(),把数组b中的数输出到文件out.dat中。
    例如:6712,6+2=7+1,则该数满足条件,
    存入数组b中。且个数cnt=cnt+1。
    8129,8+9 1=1+2.则该数不满足条件,忽略。
【试题程序】
    #inelude
    int alS00],b[300],cnt=0;
    void writeDat();
    void readDat();
    void isValue()
{
}
main()
{
  int i;
  readDat();
  jsValue();
  writeDat();
  printf(“cnt=%d\n”,cnt);
  for(i=0;i    printf(”b[%d]=%d\n”,i.b);
}
void readDat()
{
  FILE*fP;
  int i;
  fp=fopen(“in.dat”,“r”);
  for(i=0;i<300Ii++)
    fscanf(fp,“%d。”,&a);
  fclose(fp);
}
void writeDat()
{
  FILE*fp;
  int i;
  fp=fopen(“out.dat”,“W”);
  fprintf(fp,“%d\n”,cnt);
  for(i=0;i    fprintf(fp,“%d\n”.b);
  fclose(fp);
}

选项

答案void jsValue() { int i,j; /*定义变量*/ int a1,a2,a3,a4; int temp; for(i=0;i<300;i++) {al=aEi-]%10, /*数位分解*/ a2=aEi]%100/10; a3=a[i]%1000/100; a4=a[i]/1000; if(a4+a1==a3+a2) /*判断条件*/ blent++]=a[i]; } for(i=0;ib[j]) { temp=b[j], bEi]=b[j]; b[j]=temp; } }

解析
转载请注明原文地址:https://kaotiyun.com/show/0olZ777K
0

最新回复(0)