大佬看看怎么回事

P1035 [NOIP2002 普及组] 级数求和

awfguyesfbh @ 2022-11-17 23:47:15


import java.util.Scanner;
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 dongyuning @ 2022-11-18 06:58:50

j = (double)1/n;

把1换成1.0试试


by awfguyesfbh @ 2022-11-18 15:39:07

@dongyuning 还是不行噢


by dongyuning @ 2022-11-18 21:41:02

import java.util.Scanner;
public class Main {
public static void main(String[]args) {
  Scanner cin = new Scanner(System.in); 
        double k=cin.nextInt();
        double sum=0;double i;
        for(i=1;;i++)
        {
            sum+=1/i;
            if(sum>k) break;
        }
        System.out.printf("%.0f",i);
    }
}

这种试试


by awfguyesfbh @ 2022-11-19 14:51:28

@dongyuning ac了但我不明白我的问题在哪里


by dongyuning @ 2022-11-19 16:55:43

我也看不出来。。。


by awfguyesfbh @ 2022-11-19 20:08:16

@dongyuning 我懂了,最后要输出整型


by dyc2013 @ 2022-11-25 09:05:10

#include<iostream>
using namespace std;
double sum=0;
int main()
{
    double n;
    cin>>n;
    for(double i=1;i<=1000000;++i)
    {
        sum=sum+1/i;
        if(sum>=n)
        {
            cout<<i;
            return 0;
        }
    }
}

//试试这个


by hct_ @ 2022-12-29 21:53:15

@dyc2013 人家Java你搞个C艹干嘛?


|