递归code。在一系列的4/1 - 4/3 + 4/5 [英] Recursion code in series of 4/1 - 4/3 + 4/5
问题描述
我试图让一个C程序中的问题是: X = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 ....
I tried to make a C program in which the problem is: x = 4/1 - 4/3 + 4/5 - 4/7 + 4/9 ....
的停止条件是当x的值被计算在previous相互作用低于0.0001
The stop condition is when the value of "x" is calculated in the previous interaction is less than 0.0001
#include <iostream>
using namespace std;
float recursion( float n ){
int f = 0 , i = 1;
float calc = 4;
f = i%2;
if (calc == 0.0001)
return 0;
if ( f != 0 ){
calc += recursion (4/n) - recursion (4/n+2);
n += 2;
}
else
calc += recursion (4/n) + recursion (4/n+2);
n+=2;
return calc;
}
int main(){
float n , result;
cout << "Enter the value of N" ;
cin >> n;
result = recursion (n);
cout << "The value of N is ..." << result ;
}
我试图让会计师来改变符号+,以 - 或 - 到+。 我不知道为什么codito不工作。任何帮助是值得欢迎的。
I tried to get an accountant to change the sign "+" to "-" or "-" to "+". I do not know why codito not working. Any help is welcome
推荐答案
我不认为递归是必要的。这是很容易解决了迭代。 我看到了下面的模式。分母是1,3,5,7-奇数。这是我%2。 分数的符号被交替+和 - 之间 - 。使用迭代的模数2 -1或不乘以电流份额。那么+ =运算总和。
I don't think recursion is necessary. This is easily solved with iteration. I see the following pattern. The denominator is 1,3,5,7- odd numbers. That's i%2. The sign of the fraction is alternating between + and -. Use the modulus 2 of the iteration to multiply the current fraction by -1 or not. Then += to the running sum.
这篇关于递归code。在一系列的4/1 - 4/3 + 4/5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!