Anoif @ 2023-04-16 21:04:43
我咋感觉明明对的,应该不会超时的,为啥TLE啊?
求大佬指点错误以及超时原因,谢谢!
TLE代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
int n,i=1,s=0;
double a=0.0;
cin>>n;
while(a<=n){
a+=1/i;
i++;
s++;
}
cout<<s;
return 0;
}
救救孩砸吧(在线跪求)
by ___Yang__ @ 2023-04-16 21:08:21
@YingXinfei
#include<bits/stdc++.h>
using namespace std;
int main(){
double n,i=1,s=0;//必须要用double
double a=0.0;
cin>>n;
while(a<=n){
a+=1/i;
i++;
s++;
}
cout<<s;
return 0;
}
by Anoif @ 2023-04-16 21:12:59
@Yangbowen0108 谢谢大佬,您知道为什么会超时吗(就int改double就不超时了)?
by Loser_Syx @ 2023-04-16 21:16:04
@Yangbowen0108 由于int除以int=int,所以a永远不会大于n
by Anoif @ 2023-08-09 08:23:27
@Saint_ying_xtf 奥~ 明白了 ~~(但下次估计还会犯)