Description
编写函数fun(), 实现功能:根据公式s=1+1/(1+2)+1/(1+2+3)+…+1/(1+2+…+n)计算s,计算结果作为函数值返回,n通过形参传入。
Input
输入仅一行,输入正整数n的值。
Output
输出仅一行,输出s的值(小数点后保留2位有效位)。
Sample Input
3
Sample Output
1.50
Source
#include <stdio.h>
void fun(int n);
void fun(int n)
{
int i,t=0;
float s=0,x;
for ( i = 1; i <=n; i++)
{
t=t+i;
x=1./t;
s=s+x;
}
printf("%.2f",s);
}
int main()
{
int n;
scanf("%d",&n);
fun(n);
system("pause");
return 0;
}