注册 登录
编程论坛 C语言论坛

小白求问数列求和

风满楼l 发布于 2021-11-16 21:30, 1307 次点击
#include"stdio.h"
#include"math.h"
main()
{
    int s,i;
    s=0;
    i=1;
    while(i<=1000)
    {
        s=pow(-1,i+1)/(2i-1);
        i++;
     }
     printf("%d",s);
}



[Error] cannot convert '__complex__ double' to 'int' in assignment
2 回复
#2
rjsp2021-11-17 08:05
程序代码:
#include <stdio.h>

int main( void )
{
    // 计算 1/1 - 1/3 + 1/5 - 1/7 + …… - 1/1999 = 0.785148
    double s = 0;
    for( int i=0; i!=1000; ++i )
        s += (i%2==0 ? +1 : -1) / (2*i+1.0);
    printf( "%f\n", s );
}
#3
风满楼l2021-11-17 14:53
膜拜大佬
1