GMU___LXB @ 2024-10-22 20:32:46
#include<iostream>
using namespace std;
int main()
{
int k;
int n = 0;
cin >> k;
for (double s = 0; s <= k; ++n, s += 1.0 / n);
cout << n;
return 0;
}
by AndyCGM @ 2024-10-22 20:34:26
因为如果是s+=1/n,他会在1/n自己取整,然后就发现加了个积木哦。
by untitled_cpp @ 2024-10-22 20:35:34
@GMU___LXB 隐式类型转换
by heyx0201 @ 2024-10-22 20:39:01
@GMU___LXB 奇妙的c++导致的。1/n
会判定为
by dldl666 @ 2024-10-22 20:39:40
@AndyCGM @GMU___LXB 准确的说是自动向下取整
by MAGA_ @ 2024-10-23 20:18:20
b=1 s=0 a=input() k=int(a) while s <= k: s+=1/b b+=1 b-=1 print(b)
by iqiqiqiqiqiqiqiq @ 2024-11-08 23:09:34
在C++的语法中,int/int的结果是int,而且是向下取整的,只能得到整数结果,但。。。1/n在n!=1的情况下好像都是小数吧? @GMU___LXB