c语言分子编写程序,输入一个正整数n,计算1-2⼀3+3⼀5-4⼀7+5⼀9-6⼀11+.......的前n项之和。

2024-11-27 08:23:18
推荐回答(3个)
回答1:

/* 1-2/3+3/5-4/7+5/9-6/11+.......的前n项之和*/
int main(int argc, char *argv[])
{
int num_n = 1;
int i = 0;
double result = 0.0;
printf("please input a Integer:");
//scanf("%d", &num_n);
num_n = 3;
for(i=0; i {
result += ((i%2)?(-(double)(i+1)/(2*i+1)):((double)(i+1)/(2*i+1)));
}
printf("result = %f.\n", result);
return 0;
}

回答2:

#include
#include

using namespace std;

int main()
{
int n = 0;
cin>>n;
double i = 1,s= 0;
while(i {
s = s -(pow(-1,i)) * i / (2*i - 1) ;
i++;
}
cout<}

回答3:

int i,k=-1;
double s=0,a=0;
for(i=1;i<=n;i++)
{ a=-(double)(2*i-1);
s+=(double)i/a; }