每日一题——第六十八题
题目:计算sinx = x - x^3 / 3! + x^5 / 5!+… + (-1) ^ (n + 1) * x ^(2 * n - 1) / ((2 * n - 1)!)
#include<stdio.h>
#include<math.h>
long long CalculateFactorical(int n);
int main() {
int x = 2;
float sum = 0.0;
//考虑精度要求,取 n = 10
for (int n = 1; n <= 10; n++)
{
sum += pow(-1, n + 1) * pow(x, (2 * n - 1)) / CalculateFactorical(2 * n - 1);
}
printf("sin%d = %lf", x, sum);//这里为了方便演示,求sin2的值
return 0;
}
/// <summary>
/// 计算n!
/// </summary>
/// <param name="n"></param>
/// <returns></returns>
long long CalculateFactorical(int n) {
if (n < 0) return 0;
if (n == 0) return 1;
if (n > 0) return n * CalculateFactorical(n - 1);
}