输入输出没问题但全是wa

P1035 [NOIP2002 普及组] 级数求和

awfguyesfbh @ 2022-11-18 20:30:49


public class Main {

    public static void main(String[] args) {

     Scanner input = new Scanner(System.in);

     double n,k,i,j,f;
     k = input.nextDouble();
     n = 1; f = 0;
     for(i = 1;n <= i; i++) {
         j = (double)1/n;
         f = (double)(f + j);
     if(f>k) {
         System.out.println(n);
         break;
     } else {n++;}

     }
    }

}
结果没问题但全是wa

by small_Dongpo @ 2023-02-05 16:37:00

我看不太懂你的代码,但你是否看懂我的代码:

#include <iostream>
using namespace std;

int main()
{
    int k;
    cin >> k;
    double sum = 0;
    int i = 0;
    while (sum <= k)
    {
        sum += 1.0 / ++i;
    }
    cout << i;
}

这种方法可能更简单


|