全红

P1035 [NOIP2002 普及组] 级数求和

lztz0503 @ 2023-08-28 13:04:52

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

完全看不出代码哪里有问题的蒟蒻来讨论去求教了

题目传送门


by Kniqht @ 2023-08-28 13:06:32

真的不需要double吗没怎么读题盲猜


by _Glassy_Sky_ @ 2023-08-28 13:15:58

@lztz0503 n要<=cnt.


by _Glassy_Sky_ @ 2023-08-28 13:16:14

@lztz0503 cnt要开double


by lztz0503 @ 2023-08-28 13:16:48

@FZwangmuem 刑


by _Glassy_Sky_ @ 2023-08-28 13:19:40

@lztz0503 cnt要初始化为0.0


by _Glassy_Sky_ @ 2023-08-28 13:23:06

@lztz0503 你这玩意有点玄学,要不参照参照我的?


by lztz0503 @ 2023-08-28 13:23:10

#include <bits/stdc++.h>
using namespace std;
int n;
double cnt = 0.0;
int i = 1;
int main(){
    cin >> n;
    while(n <= cnt){
        cnt += 1.0 / i;
        i += 1;
    }
    cout << i;
    return 0;
}

……累了

为什么还是不行啊o.O


by _Glassy_Sky_ @ 2023-08-28 13:24:05

@lztz0503 你看看我的

#include<bits/stdc++.h>
using namespace std;
int main()
{
    int k;
    double sum = 0.0;
    cin >> k;
    for(int i = 1; ; i ++)
    {
        if(sum > k)
        {
            cout << i - 1;
            return 0;
        }
        sum += double(1.0 / i);
    }
    return 0;
}

by _Glassy_Sky_ @ 2023-08-28 13:25:10

@lztz0503 你的代码连这样都过不了:

#include <bits/stdc++.h>
using namespace std;
int main()
{
    double n;
    double i = 1.0;
    double cnt = 0.0;
    cin >> n;
    while(n <= cnt)
    {
        cnt += double(1.0 / i);
        i += 1.0;
    }
    cout << i + 1;
    return 0;
}

by LittleAcbg @ 2023-08-28 13:25:45

@lztz0503 最后要输出 i-1,而不是 i


| 下一页