SDFZ_SHH @ 2023-10-15 12:16:29
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
double a,b;
cin >> k;
b = double(k) - 1.0;
for (int i = 2;i <= 17;i ++)
{
if (b == 0.0)
{
cout << i;
break;
}
else
{
b = b - (1.0 / i);
continue;
}
}
return 0;
}
by DevilsFlame @ 2023-10-15 12:35:42
你的我有些看不懂 这是我的代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int ans,k;
double h=0;
cin>>k;
for(int i=1;h<=k;i++){
h+=double(1.0/i);
ans=i;
}
cout<<ans<<endl; return 0;
}
by Ssim_on_wxs @ 2023-10-15 13:02:29
下面是样例:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
cin >> k;
if (k == 1)//特判 k为1时,直接输出2
{
cout << "2";
}
for (double i = 0;i <= k;)//只要i小于k,就让i加上1/n
{
++ n;
i += 1.0 / n;
}
cout<<i;
return 0;
}
by Ssim_on_wxs @ 2023-10-15 13:04:41
你这个逻辑就不对啊,ab完全没有用,很多问题,建议重构
by _llch_ @ 2023-10-18 12:07:55
by _llch_ @ 2023-10-18 12:18:27
@luchuhan 数据范围中的
by YU15050299395 @ 2023-10-18 22:48:27
@Ssim_on_wxs 你这个代码有错误 @Ssim_on_wxs
by Ssim_on_wxs @ 2023-10-18 23:40:49
@YU15050299395 抱歉,感谢指正 我是按着楼主的复制然后修改的,正确应该是这样
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n,k;
double i = 0;
cin >> k;
if (k == 1)
{
cout << "2";
}
for (;i <= k;)
{
++ n;
i += 1.0 / n;
}
cout<<i;
return 0;
}
by Ssim_on_wxs @ 2023-10-18 23:44:57
@YU15050299395 错位有点问题,可以自行修改