怎样用C++ 求一分数2⼀1+3⼀2+5⼀3+8⼀5+13⼀8+21⼀13......的值?

2024-12-28 23:11:58
推荐回答(2个)
回答1:

float a, b, s, t;
int i;
a = 2; b = 1; s = 0;
for(i = 0; i < 6; i++ )
{
s += a/b;
t = a;
a = a+b;
b = t;
}
s为结果,改变循环次数就可以增减相加项。

回答2:

//怎样用C++ 求一分数2/1+3/2+5/3+8/5+13/8+21/13......的值?
double Add(int n){
double totle=0;
int Fenzi=2,Fenmu=1;
for(int i=0;i totle+=(double)Fenzi/(double)Fenmu;
int t=Fenzi;
Fenzi+=Fenmu;
Fenmu=t;
}
printf("Value=%f\n",totle);
return totle;
}