解决方案
80
#include<stdio.h> void main() { int i, n, flag; double x, item, sum = 0.0; double fact(int n); double power(double x, int n); printf("输入一个正整数n和一个实数x:"); scanf("%d%lf", &n, &x); for(i = 0; i < n; i++) { flag = (i % 2 == 0) ? 1 : -1; item = flag * power(x, 2 * i) / fact(2 * i); sum += item; } printf("cos(x)的近似值=%0.4f\n", sum); } double fact(int n) { double product = 1.0; int i; for(i = 2; i <= n; i++) product *= i; return product; } double power(double x, int n) { if(n == 0) return 1.0; double pow = x; int i; for(i = 1; i < n; i++) pow *= x; return pow; }