//计算并输出S=1+(1+2^0.5)+(1+2^0.5+3^0.5)...+(1+2^0.5+3^0.5+...+n^0.5)

 #include<math.h>
#include<stdio.h>
double fun(int n)
{
double s=,S=;
for (int i = ; i <= n+; i++)//这里判断条件为n+1.
{
S += s;
s += sqrt(i);//2^0.5=2^(1/2)=根号下2=sqrt(2)
printf("%f\n", S);
printf("%f\n", s);
}
return S;
}
void main()
{
FILE *wf;
int n;
double s;
printf("\n\nInput n: ");
scanf("%d",&n);
s=fun(n);
printf("\n\ns=%f\n\n",s);
/******************************/
wf=fopen("out.dat","w");
fprintf(wf,"%f",fun());
fclose(wf);
/*****************************/
}

//使用递归解决:

 double f(int n)
{
if (n == ) return ;
else return sqrt(n) + f(n - );
}
double fun(int n)
{
double S=;
for (int i = ; i<=n; i++)
{
S += f(i);
}
return S;
}

//也可以使用pow函数。功能:求x的y次方。用法:pow(x,y)。

05-11 15:08