P1035 题解

· · 题解

题目传送门

思路

用一个 while 循环进行模拟,循环条件为 S_n\le k。用一个计数器 C 统计次数。每一次执行次数加一,即 C\gets C+1,再更新 S_n\gets\frac{1}{C}。最后输出 C 即可。

注意事项

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;
}