#include
main()
{
float numerator = 1, denominator = 2, temp;
float total = 0;
int num;
total += 1;//第一项
for( num=0; num<19; num++ )//后十九项
{
total += numerator / denominator;
temp = denominator;
denominator += numerator;
numerator = temp;
}
printf("total = %f\n",total);
}
VC编译通过,希望对你有点帮助。
main()
{
int a,b,i,t;
double sum,x;
a = 1;
b = 1;
sum = 1.0;
for(i = 1;i < 20;i++){
x = b /(double)(a + b);
sum += x;
t = b;
b = a + b;
a = t;
}
printf("Total:%f",sum);
getch();
}
稍微做了修改
TC版:
main()
{
int a,b,i,t;
double sum,x;
a = 1;
b = 1;
sum = 1.0;
for(i = 1;i < 20;i++){
x = b /(a + b);
sum += x;
t = b;
b = a + b;
a = t;
}
printf("Total:%f",sum);
getch();
}