#include stdio.h
成都創(chuàng)新互聯(lián)從2013年成立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目成都網(wǎng)站建設(shè)、成都網(wǎng)站制作網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢(mèng)想脫穎而出為使命,1280元林芝做網(wǎng)站,已為上家服務(wù),為林芝各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:028-86922220
#include math.h
int main(void)
{
double f = 1;
double x, k, x2, power = 1;
int i = 2;
scanf("%lf", x);
power += x;
k = x;
do {
x2 = power;
f *= i++;
x *= k;
power += x / f;
} while(fabs(power-x2) 1e-8);
printf("%f", power);
}
/////////////////////////
你那個(gè)代碼,1、pow函數(shù)可以不用自己寫(xiě),你寫(xiě)的精度也不夠;2、保存階乘最好用double,不然要溢出。
修正了以上2點(diǎn)就沒(méi)問(wèn)題了,代碼如下:
#include stdio.h
#include math.h
double f1(int n)
{
double s = 1;
int i;
for ( i=1; i=n; i++)
s *= i;
return s;
}
main()
{
int x,i, n;
double ex = 1;
scanf("%d%d",x,n);
for ( i=1; i=n; i++)
ex += pow(x, i) / f1(i);
printf("%lf %lf\n",ex, exp(x));
}
#include stdio.h
double fun(double x,int n)
{
double result = 1.0;
double item = 1.0;
int i;
for (i = 1; i = n; i++)
{
item = item * x / i;
result += item;
}
return result;
}
int main()
{
double x;
int n;
scanf("%lf%d",x,n);
printf("%lf\n",fun(x,n));
}
1+3+5+...+(2n-1) = n(1+2n-1)/2 = n^2
1^2+2^2+3^2+n^2 = n(n+1)(2n+1)/6
所以編程實(shí)現(xiàn)的話如下:
#include?"stdio.h"
int?main?()
{
int?n,ret;
printf("please?input?a?integer?n:");
scanf("%d",n);
while(n??0)
{
ret?=?n*(n+1)*(2*n+1)/6;
printf("result?:?%d\n",ret);
printf("please?input?a?integer?n:");
scanf("%d",n);
}
return?0;
}