小编Sin*_*ina的帖子

什么导致我的c ++代码中的错误?

我尝试编写一个代码,它应该计算4 - 4/3 + 4/5 - 4/7 + 4/9 + ...但它仍然打印"3"作为答案.

#include <iostream>
#include <math.h>
#include <conio.h>

using namespace std;

int main()
{
    int s=0,a,n;
    cin>>n;
    for(int i=0 ; i<=n ; i++)
    {
        a=(4/((2*i)+1))*pow(-1,i);
        s=s+a;
    }
    cout<<s;
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++

-1
推荐指数
1
解决办法
68
查看次数

标签 统计

c++ ×1