2006年9月全國等級考試三級c語言上機(jī)題庫(二十二)

字號:


    ★題目22(無憂id 39 平方根問題)
    請編寫函數(shù)countValue(),它的功能是:求n以內(nèi)(不包括n)同時能被3與7整除的所有自然數(shù)之和的平方根s,并作為函數(shù)值返回,最后結(jié)果s輸出到文件out.dat中。
    例如若n為1000時,函數(shù)值應(yīng)為:s=153.909064。
    部分源程序存在文件prog1.c中。
    請勿改動主函數(shù)main()和輸入輸出數(shù)據(jù)函數(shù)progReadWrite()的內(nèi)容。
    #include
    #include
    #include
    double countValue(int n)
    { int i;
    double s=0.0;
    for(i=1;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);
    }