C语言 求高手 题目是 1-1/2+1/3-1/4+1/5-1/6+1/7……+1/2n-1 怎么做啊?我的这个程序哪有问题?
#include<stdio.h>intmain(){inti,j,s=1,n;scanf("%d",&n);if(n>=2){for(j=2;j<=2;j++)i=-1...
#include <stdio.h>
int main()
{
int i,j,s=1,n;
scanf("%d",&n);
if(n>=2)
{
for(j=2;j<=2;j++)
i=-1/(2*j-2)+1/(2*j-1);
s+=i;
}
printf("%d\n",s);
return 0;
} 展开
int main()
{
int i,j,s=1,n;
scanf("%d",&n);
if(n>=2)
{
for(j=2;j<=2;j++)
i=-1/(2*j-2)+1/(2*j-1);
s+=i;
}
printf("%d\n",s);
return 0;
} 展开
3个回答
展开全部
变量的定义有些问题
#include <stdio.h>
int main()
{
int j,n;
double s = 1.0,i;
scanf("%d",&n);
if(n>=2)
{
for(j=2;j<=n;j++)
i=-1.0/(2*j-2)+1.0/(2*j-1);
s+=i;
}
printf("%lf\n",s);
return 0;
}
#include <stdio.h>
int main()
{
int j,n;
double s = 1.0,i;
scanf("%d",&n);
if(n>=2)
{
for(j=2;j<=n;j++)
i=-1.0/(2*j-2)+1.0/(2*j-1);
s+=i;
}
printf("%lf\n",s);
return 0;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2012-06-11
展开全部
#include <stdio.h>
int main()
{
double num1=0,num2=1,temp1=1;
printf("please input the num!");
while (1)
{
scanf("%d",&num1);
if(num1 < 0)
{
printf("Please input the number of greater than zero!\n");
}
else
if((num1 == 0) || (num1 == 1))
{
num1 = 1;
printf("The answer is:%d\n",num1);
return 0;
}
else
{
continure;
}
}
for(num2;num2<2*num1 - 1;num2++)
{
if(num%2 == 0)
{
temp1 -= (1/num2);
}
else
{
temp1 += (1/num2);
}
}
printf("The answer is :%d\n",temp1);
return 0;
}
没编译器 好久没用了 应该是这样, 错了的话 可以看看思路
int main()
{
double num1=0,num2=1,temp1=1;
printf("please input the num!");
while (1)
{
scanf("%d",&num1);
if(num1 < 0)
{
printf("Please input the number of greater than zero!\n");
}
else
if((num1 == 0) || (num1 == 1))
{
num1 = 1;
printf("The answer is:%d\n",num1);
return 0;
}
else
{
continure;
}
}
for(num2;num2<2*num1 - 1;num2++)
{
if(num%2 == 0)
{
temp1 -= (1/num2);
}
else
{
temp1 += (1/num2);
}
}
printf("The answer is :%d\n",temp1);
return 0;
}
没编译器 好久没用了 应该是这样, 错了的话 可以看看思路
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询