!!!C问题在线等

来源:百度知道 编辑:UC知道 时间:2024/05/21 08:54:46
!!!C问题在线等
求1-1/2+1/3-1/4+1/5-1/6........1/n的值
#include"math"
main()
{int n=0,k;
double s=0;
scanf("%d",&k);
for(i=1;i<=k;i++)
{s+=pow(-1,n)*1/i;n++;}
printf("%d",s);
}
能通过编译,但得到的值不对,哪位DD帮我看看问题在哪!!!!!!

先申明,我是新手,下面是我写的.
#include "math.h"
#include "stdio.h"
void main()
{int i,k;
double s=0;
scanf("%d",&k);
for(i=1;i<=k;i++)
{
s+=((i%2==0)?-(1.0/i):1.0/i);
}
printf("%f",s);
}

#include <math.h>
#include <stdio.h>

缺少#include <stdio.h>头文件

而且printf("%d",s);不对吧
应该是printf("%f",s);
s是浮点数啊

这道题本身很简单
但是不知道你用的什么编译器
你要是用vc++6.0我可以帮你写一下