表达式为1 - x ^ 2 *(1/3 - x ^ 2/2 *(1/5 - x ^ 2/3 *(1/7 - ...))) 我以某种方式编写程序,以便您可以输入x和N的值
int k=0
double expression(double x, int k, int N)
if(k>N) return 0;
return 1/(2*k+1)-x*x/(k+1)*expression(x,k+1,N);
问题是循环会像计算一样 1 - (x ^ 2 * 1/3) - (x ^ 2/2 * 1/5) - (x ^ 2/3 * 1/7) - ......
如何更改我的代码以使其正常工作?
答案 0 :(得分:1)
试试这个:
//Extract billing address from ABRecord format and assign accordingly
let addressProperty: ABMultiValue = ABRecordCopyValue(billingAddress, kABPersonAddressProperty).takeUnretainedValue() as ABMultiValue
if let dict: NSDictionary = ABMultiValueCopyValueAtIndex(addressProperty, 0).takeUnretainedValue() as? NSDictionary {
print(dict[String(kABPersonAddressStreetKey)] as? String)
print(dict[String(kABPersonAddressCityKey)] as? String)
print(dict[String(kABPersonAddressStateKey)] as? String)
print(dict[String(kABPersonAddressZIPKey)] as? String)
print(dict[String(kABPersonAddressCountryKey)] as? String) //"United States"
}
注意double expression(double x2, int k, int N){
if(k>N)
return 0;
return 1.0/(2*k+1)-x2/(k+1)*expression(x2,k+1,N);
}
double funct(double x, int N){
int k=0;
double x2 = x * x;
return expression(x2, k, N);
}
应为1
这样的事实,以避免整数除法。
答案 1 :(得分:0)
对于给定的表达式 1 - x ^ 2 *(1/3 - x ^ 2/2 *(1/5 - x ^ 2/3 *(1/7 - ...)))
您想首先计算 x * x /(k + 1)* innersum()。把表达放在括号中就可以了。它也是k+1
而不是k-1
。
int k=0
double expression(double x, int k, int N)
if(k>N) return 1;
return 1/(2*k+1)-x*x*(1/(k+1)*expression(x,k+1,N));