思路
用一个 while 循环进行模拟,循环条件为 。用一个计数器 统计次数。每一次执行次数加一,即 ,再更新 。最后输出 即可。
注意事项
- 为
double类型, 计算时应先转为double。
AC CODE
#include<bits/stdc++.h>
using namespace std;
int read(){int x=0;char f=1,ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();return x*f;}
int main(){
int k=read(),cnt=0;
double n=0;
while(n<=k){
++cnt;
n+=1.0/cnt;
}
printf("%d\n",cnt);
return 0;
}