TIMEpings @ 2017-11-03 20:09:43
#include<stdio.h>
using namespace std;
int main()
{
int k,n;
double sn=0.000000;
double t;
scanf("%d",&k);
for(n=1;;n++)
{
t = 1/n;
sn += t;
if(sn>k)
{
printf("%d",sn);
break;
}
}
return 0;
}
编译器都拯救不了的程序啊!!为什么会这样啊!!!!
by _LiM @ 2017-11-03 20:18:04
你double类型直接可以弄成全局
by _LiM @ 2017-11-03 20:18:57
然后你输出的东西都是错的,
by _LiM @ 2017-11-03 20:21:24
#include<stdio.h>
using namespace std;
double sn;
double t;
int k,n;
int main()
{
scanf("%d",&k);
while(sn<=k)
{
n++;
sn+=1.0/double(n);
}
printf("%d",n);
return 0;
}
我帮你轻轻一改就是AC解了
自己看一看,对比一下
by WolfBite @ 2017-11-03 20:47:32
t = 1/n;
两个整数相除是整数哦
所以1/n 当n大于2时就一直是0了
by _LiM @ 2017-11-03 21:37:22
@WolfBite 对 还有 楼主输出的是Sn
by TIMEpings @ 2017-11-04 12:35:00
@LiM_817 谢谢dalaoQWQ
by TIMEpings @ 2017-11-04 12:40:16
@WolfBite 啊!感谢!