aaaa___ @ 2018-07-25 19:26:33
using namespace std; int main() { int n=0,t=0,k; double Sn; scanf("%d",k); while(Sn<=k) {
++n;
t=1/n;
Sn+=t;
} printf("%d",n); return 0; }
by Smile_Cindy @ 2018-07-26 15:23:10
@列王纷争 你Sn要置0
by aaaa___ @ 2018-07-26 18:10:58
@Alpha 哦哦 谢谢 我去试试!
by aaaa___ @ 2018-07-26 19:02:00
@Alpha 这个也试过了 还是一样
by aaaa___ @ 2018-07-26 19:13:19
@Alpha 是不是定义DOUBLE量中的0有什么要特别注意的?
by Smile_Cindy @ 2018-07-27 09:01:20
@列王纷争 Sn要置零
by Smile_Cindy @ 2018-07-27 09:01:50
@列王纷争 如果不置零,Sn的值就是随机的
by aaaa___ @ 2018-07-27 21:11:53
@Alpha #include<cstdio> using namespace std; int main() { int n=0,k; double Sn=0,t=0; scanf("%d",k); while(Sn<=k) { n++; t=1.0/n; Sn+=t; } printf("%d",n); return 0; }
by Smile_Cindy @ 2018-07-27 21:37:51
@列王纷争 错了,应为:scanf("%d",&k)
by Smile_Cindy @ 2018-07-27 21:38:49
@列王纷争
#include<cstdio>
using namespace std;
int main()
{
int n=0,k;
double Sn=0,t=0;
scanf("%d",&k);
while(Sn<=k)
{
n++;
t=1.0/n;
Sn+=t;
}
printf("%d",n);
return 0;
}
实测AC
by aaaa___ @ 2018-07-29 22:09:38
@Alpha 请问这两个有什么区别吗?