为什么TLE

P1035 [NOIP2002 普及组] 级数求和

Romanrose @ 2024-01-21 21:25:54

#include <iostream>
#include <iomanip>
using namespace std;

int main(){//P1035 [NOIP2002 普及组] 级数求和
    int k;
    cin >> k;
    int n = 1;
    double sum = 1;

    while(sum <= k){
        sum = sum + (double)1 / n;
        n++;
    } 
    cout << n;

    return 0;
}

by Dream_Creator @ 2024-01-21 21:32:04

@Romanrose

#include <iostream>
#include <iomanip>
using namespace std;

int main(){//P1035 [NOIP2002 普及组] 级数求和
    int k;
    cin >> k;
    int n = 1;
    double sum = 0.0;

    while(sum <= k){
        sum = sum + (double)(1.0 / n);
        if(sum>k) break;
        n++;
    } 
    cout << n;

    return 0;
}

by Romanrose @ 2024-01-21 21:47:13

谢谢你


|