c++,这个怎么错了?(TLE)

P1035 [NOIP2002 普及组] 级数求和

杜雨峰 @ 2020-03-07 11:04:59

#include <iostream>
using namespace std;

int main() 
{
    int i,n,k,m=1;
    cin>>k;
    for(i=2;m<k;i++)
    {
        m+=1/i;
    }
    cout<<i;
    return 0;
}

by Flash1ng @ 2020-03-07 11:07:22

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

by 杜雨峰 @ 2020-03-07 11:07:32

求回复


by jijidawang @ 2020-03-07 11:08:19

@杜雨峰 用double


by jijidawang @ 2020-03-07 11:08:39

@杜雨峰 你改成1.0/i,要不然整形都是0


by SIXIANG32 @ 2020-03-07 11:11:09

@杜雨峰 整形您敢做?


by 杜雨峰 @ 2020-03-07 11:14:12

谢谢大家!


by Clintikas97 @ 2020-03-07 11:21:09

double了解一下


|