5 条题解

  • 1
    @ 2023-5-17 17:16:33

    由于 ++n 是为下一次准备的分母,因此最后一次会多加 1,所以输出时自减。

    #include <bits/stdc++.h>
    using namespace std;
    int main() {
        int k, n = 1;
        double S = 0;
        cin >> k;
        while(S <= k) {
            S += 1.0 / n;
            ++n;
        }
        cout << --n << endl;
        return 0;
    }
    
    • 0
      @ 2024-3-3 15:53:52

      #include <bits/stdc++.h> using namespace std; int main() { int k, n = 1; double S = 0; cin >> k; while(S <= k) { S += 1.0 / n; ++n; } cout << --n << endl; return 0; }

      • -1
        @ 2023-11-29 21:14:36

        以前写的

        #include<bits/stdc++.h>
        using namespace std;
        double sum=0,n;
        int main(){  
            cin>>n;
            for(int i=1;;i++){
                sum+=(double)(1.0/i);
                if(sum>n){
                    cout<<i;
                    return 0;
                }
            }
        }
        
        • -1
          @ 2023-10-18 19:02:39

          至少一年前写的代码

          #include <bits/stdc++.h>
          using namespace std;
          int main()
          {
              int k;
              cin>>k;
              int n = 1;
              double sum = 0;
              while (true)
              {
                  sum += 1.0 / n;
                  if (sum >= k)
                  {
                      break;
                  }
                  n += 1;
              }
              cout<<n;
              return 0;
          }
          
          • -1
            @ 2022-6-12 10:29:54
            #include <bits/stdc++.h>
            using namespace std;
            int main()
            {
                double s=0;
                int n,i=1;
                scanf("%d",&n);
                while(s<n)
                {
                	s+=1.0/i;
                	i++;
            	}
            	printf("%d",--i);
            	return 0;
            }
            
            • @ 2022-10-20 15:14:05

              大佬,为什么i要自减?

          • 1

          信息

          ID
          36
          时间
          1000ms
          内存
          125MiB
          难度
          1
          标签
          递交数
          344
          已通过
          163
          上传者