while循环,全超时

P1035 [NOIP2002 普及组] 级数求和

Mxolq_Icb @ 2023-02-06 21:19:01

#include<iostream>
#include<iomanip>
#include<cmath>
#include<algorithm>
#include<string>
using namespace std;
int main()
{
    double sum;int n,k;cin>>k;
    while(1){
        int i=1;i++;
        sum+=1/i;
        if(sum>k){
            cout<<i;
            return 0;
        }
    }
    return 0;
}

by Zan_mao @ 2023-02-06 21:42:40

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

1、浮点问题 2、重复初始化i


|