哪里错了?

P1035 [NOIP2002 普及组] 级数求和

wh_duyuhao @ 2024-11-27 19:28:46

#include<iostream>
using namespace std;
int main(){
    int k;
    cin>>k;
    int n=0,i=0;
    double ans=0;
    while(1){
        ans+=1.0/i;
        n++;
        if(ans>k) break;
        i++;
    }
    cout<<n;
    return 0;
}

????????????????????????


by PengRuiXuan @ 2024-11-27 19:32:18

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

by wh_duyuhao @ 2024-11-27 19:32:33

求回!!!!!!!


by wh_duyuhao @ 2024-11-27 19:33:11

666,又是答案。


by wh_duyuhao @ 2024-11-27 19:33:47

求大佬帮我点明错误!


by PengRuiXuan @ 2024-11-27 19:34:57

@wh_duyuhao

OK等一下。


by wangjingxi_ @ 2024-11-27 19:37:48

#include<iostream>
using namespace std;
int main()
{
    long double n=0,k,sum=0;
    cin>>k;
    while(sum<=k)
    {
        sum+=(1.0/(n+1));
        n++;
    }
    cout<<n;
    return 0;
}

对了一下,好像也没问题,你把这个代码交一下试试。


by PengRuiXuan @ 2024-11-27 19:39:03

把n和ans的初始值改成1可以过样例,但是只能过一个测试点。


by wh_duyuhao @ 2024-11-27 19:40:11

我知道,因为我认为这个发出来的代码应该全对!


by wh_duyuhao @ 2024-11-27 19:41:40

@wangjingxi_ ???全对? 咱们两个代码改一下是一样的啊!


by wh_duyuhao @ 2024-11-27 19:43:51

我的“i”设置的值是“0”!


| 下一页