libin13815498262 @ 2023-02-06 12:57:41
#include<bits/stdc++.h>
using namespace std;
float sum(int i)
{
float z;
z=1/i;
return z;
}
int main()
{
int k,x,i;
x=0;
cin>>k;
for(i=1;x<k;i++)
{
x+=sum(i);
}
cout<<i<<endl;
return 0;
}
by RP_INT_MAX @ 2023-02-06 13:01:08
@libin13815498262 乐。你确定 x 不用 float???
by Coffins @ 2023-02-06 13:01:35
x改成float,要不然就自动转型了
by libin13815498262 @ 2023-02-06 13:07:01
@Coffins 谢谢大哥,我改了下数型转换,确实可以了
by libin13815498262 @ 2023-02-06 13:07:15
@RP_INT_MAX 谢谢大哥,我改了下数型转换,确实可以了
by libin13815498262 @ 2023-02-06 13:09:22
#include<bits/stdc++.h>
using namespace std;
int main()
{
float k,s=0,i;
cin>>k;
for(i=1;s<k;i++){
s+=1/i;
}
cout<<i<<endl;
return 0;
}
新问题,这段代码可以执行的出来,但五个测试点全红,不是很明白哪里出了问题
by RP_INT_MAX @ 2023-02-06 13:11:37
@libin13815498262 两个问题
float 换 double
输出的是 i-1
by libin13815498262 @ 2023-02-06 13:21:18
@RP_INT_MAX 谢谢大哥,一语点醒梦中人了
by JoyBoy521 @ 2023-02-28 19:31:11
@RP_INT_MAX 为啥要i-1