#include
int main()
{
int n;
float sum=1;
scanf("%d", &n);
int a=1,b=1,c;
if (n == 1)
sum=1;
else
while (--n != 0)
{
c=a+b;
sum+=(float)b/(float)c;
a=b;
b=c;
}
printf("%f\n", sum);
return 0;
}
代码如下:
#include
void main() {
float i,j,n,m,result=0;
scanf("%d",&n);
for(m = 1,i = 1,j = 1; m<=n; m++) {
int tmp;
result += i/j;
tmp = j;
j = i + j;
i = tmp;
}
printf("result is %d",result);
}
#include
void main()
{
int i, j, k, n, t;
float sum;
printf("input n:");
scanf("%d",&n);
i = 1;
j = 2;
sum = 0;
for (k = 1; k <=n; k++)
{
sum += (float)i/(float)j;
t = i + j;
i = j;
j = t;
}
printf("sum = %f\n", sum);
}