2006年9月全國(guó)等級(jí)考試三級(jí)c語(yǔ)言上機(jī)題庫(kù)(二十二)
★題目22(無(wú)憂id 39 平方根問題) 請(qǐng)編寫函數(shù)countvalue(),它的功能是:求n以內(nèi)(不包括n)同時(shí)能被3與7整除的所有自然數(shù)之和的平方根s,并作為函數(shù)值返回,最后結(jié)果s輸出到文件out.dat中。例如若n為1000時(shí),函數(shù)值應(yīng)為:s=153.909064。部分源程序存在文件prog1.c中。請(qǐng)勿改動(dòng)主函數(shù)main()和輸入輸出數(shù)據(jù)函數(shù)progreadwrite()的內(nèi)容。#include <conio.h>#include <math.h>#include <stdio.h> double countvalue(int n){ int i;double s=0.0;for(i=1;i<n;i++)if(i%21==0) s+=i;return sqrt(s);} main(){clrscr();printf("自然數(shù)之和的平方根=%f\n",countvalue(1000));progreadwrite();} progreadwrite(){file *fp,*wf;int i,n;float s; fp=fopen("in.dat","r");if(fp==null){printf("數(shù)據(jù)文件in.dat不存在!");return;}wf=fopen("out.dat","w");for(i=0;i<10;i++){fscanf(fp,"%d\n",&n);s=countvalue(n);fprintf(wf,"%f\n",s);}fclose(fp);fclose(wf);}