编写函数fun():S=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+3+…+n)
编写函数fun():S=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+3+…+n)
编写函数fun():S=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+3+…+n)
编写函数fun():S=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+3+…+n)
用什么语言呢?我就当是c++了.改成C也好改.一般也都能看懂.前面是测试用的主函数,后面是函数.
#include "stdafx.h"
double fun(int n);
int main()
{
\x05int n;
\x05n=3;
\x05double c;
\x05c=0;
\x05c=fun(n);
\x05printf("%f",c);
\x05getchar();
\x05return 0;
}
double fun (int n)
{
double sum=0;
double temp=0;
double temp_1;
for(int i=1;i
#include
#include
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;
...
全部展开
#include
#include
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;
}
main()
{
int n;
float s;
printf("\nPlease enter N:");
scanf("%d",&n);
s=fun(n);
printf("The result is: %f\n",s);
}
收起