详细代码如下:
#include<conio.h>
#include<stdio.h>
#include<math.h>
#include<stdlib.h>
double fun(double x, int n)
{
/*
analyse:
*/
/*
Standard Answer:
int i ;
double s = 1.0, sl = 1.0;
for(i = 1; i <= n; i++)
{
sl = sl *i; //各项中的阶乘
s = s + pow(x,i)/sl; //按公式求出
}
return s;
*/
double s =1.0;
int i,j;
double up =1.0;
double down =1.0;
//
for(i=1; i<=n; i++)
{
up *= x;
down =1 ; //清除上次down 值 !!!!
for(j = 1; j <= i; j++)
{
down *= j;
}
s += up/down;
}
return s;
}
void main()
{
FILE *wf;
system("CLS");
printf("%f ",fun(0.3,10));
/******************************/
wf=fopen("out.dat","w");
fprintf(wf,"%f",fun(0.3,10));
fclose(wf);
/*****************************/
}