TLE怎么办!!!???

P1035 [NOIP2002 普及组] 级数求和

eat_melon_ikun @ 2024-06-08 20:27:05

超时怎么办!??
TLE超时网址查看

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

by JYW2011 @ 2024-06-08 20:29:09

李世民:超时了就给朕斩了


by ikunTLE @ 2024-06-08 20:31:05

首先你不应该用int来存答案


by ikunTLE @ 2024-06-08 20:32:45

其次你的s应该初始化为 0


by NINT @ 2024-06-08 20:34:28

@ikunTLE 补充:输出的也应该是 n-1 才对


by ikunTLE @ 2024-06-08 20:35:17

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

by ikunTLE @ 2024-06-08 20:36:06

@eat_melons_ikun AC了


by eat_melon_ikun @ 2024-06-09 15:36:37

@ikunTLE 谢谢


by eat_melon_ikun @ 2024-06-09 15:36:59

@JYW2011 6


|