我试着编写一个代码,它应该计算4 - 4/3 + 4/5 - 4/7 + 4/9 + ... 但它仍然打印“3”作为答案。什么导致我的c + +代码中的错误?
#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;
}
你对'n'的价值是什么? – shafeen
每个数字都有一个从0到无穷大的ID。作为一个例子,如果你设置2为“n”的值,它会计数4 - 4/3 – Sina
conio.h是从恐龙时间 –