求s=1+1/(1+2)+1/(1+2+3)….+1/(1+2+3….+n)的值
#include <stdio.h>
float fun(int n)
{
int i,s1=0;
float s=0.0;
for(i=1;i<=n;i++)
{s1=s1+i; /求每一项的分母/
s=s+1.0/s1; /求多项式的值/
}
return s;
}
void main()
{ int n; float s;
void NONO ( );
printf("\nPlease enter N:"); scanf("%d", &n);
s = fun(n);
printf("the result is: %f\n", s);
NONO();
}
void NONO ( )
{/ 本函数用于打开文件,输入数据,调用函数,输出数据,关闭文件。 /
FILE fp, wf ;
int i, n ;
float s;
fp = fopen("in.dat","r") ;
wf = fopen("out.dat","w") ;
for(i = 0 ; i < 10 ; i++) {
fscanf(fp, "%d", &n) ;
s = fun(n) ;
fprintf(wf, "%f\n", s) ;
}
fclose(fp) ;
fclose(wf) ;
}