问题 问答题

请编写函数countValue(),其功能是:求n以内(不包括n)同时能被3与7整除的所有自然数之和的平方根s,并作为函数值返回。
主函数最后调用函数progReadWrite()从in.dat文件中读取10组数据,分别得出结果,且把结果输出到out.dat文件中。
例如:若n为1000时,函数值应为:s=153.909064。
注意:部分程序已经给出。
请勿改动主函数main()和输入输出数据函数progReadWrite()的内容。
#include<conio.h>
#include<math.h>
#include<stdio.h>
double countValue(int n)


void progReadwrite()

FILE*fp,*wf;
int i,n;
float s;
fp=fopen("in.dat","r");
if(fp==NULL)

printf("数据文件in.dat不存在!");
return;

wf=fopen("out.dat","w");
for(i=0; i<10; i++)

fscanf(fp,"%d,",&n);
s=(float)countValue(n);
fprintf(wf,"%f\n",s);

fclose(fp);
fclose(wf);

main()

printf("1000以内符合条件的自然数之和的平方根=%f\n",countValue(1000));
progReadWrite();

答案

参考答案:

double countVahe(int n)

{

double xy=0.0; /*初始化double型变量xy以作累加之用*/

int i;

for(i=A;i<n;i++)/*循环遍历从A到n-A之间的整数*/

if(i%C==0&&i%G==0)

xy+=i; /*若当前数既能被C整除又能被G整除,则累加到变量xy中*/

xy=sqrt((double)xy);/*求累加和的平方根*/

return xy;

}

填空题
多项选择题