为什么自己编译器上能跑出正常结果,但是在这里一直报错...

P1035 [NOIP2002 普及组] 级数求和

Donecancel @ 2023-10-24 09:28:23

#include<stdio.h>
int main()
{
    double c;
    int k, n;
    scanf_s("%d", &k);
    n = 0;
    for (c = 0; c <= k; ++n, c = c + 1.0 / n);
    printf("%d", n);
    return 0;
}

by zhangbo1000 @ 2023-10-24 09:54:53

scanf_s不是c++标准中的,请用scanf


by penguin_is_cool @ 2023-10-24 17:03:37

#include <iostream>
#include <cmath>
using namespace std;
int main(){
    double n;
    cin>>n;
    double sum=0;
    for (int i=1.0;i<100000000000;i++){
        sum+=1.0/i;
        if (sum>n){
            cout<<i;
            break;
        }
    }
}

while循环可能会更好一些,但我用了for循环


by Donecancel @ 2023-10-28 10:25:16

@zhangbo1000 解决了,多谢


|