如何“摆脱”TLE?

P1035 [NOIP2002 普及组] 级数求和

Plum_Steven @ 2022-02-04 12:40:20

#include <iostream>
using namespace std;

int main()
{
    int n=1, k;
    float total;
    cin >> k;

    while(1)
    {
        total += 1 / n;
        n++;
        if (total > k)
        {
            break;
        }
    }
    cout << total;
    return 0;
}

by y0y68 @ 2022-02-04 12:43:19

@Plum_Steven

total+=1.0/n;
cout<<n;

by Plum_Steven @ 2022-02-04 12:47:07

回复@y0y68: 现在全部变成了WA


by 看什么看 @ 2022-02-04 12:53:07

@Plum_Steven

double total = 0;

cout << n-1;

by Inscription_Assassin @ 2022-02-04 13:03:18

拜托TLE的最好办法就是数组再开大点变成MLE


by Nevergonna_CCF @ 2022-02-04 13:04:08

楼上正解


by Inscription_Assassin @ 2022-02-04 13:04:22

如果你觉得这个办法不好就把数据开到a[0],变成RE。你康康,一下子两个解决方案


by Nevergonna_CCF @ 2022-02-04 13:10:08

@Inscription_Assassin 或者无脑输出I Love Luogu,WA


by CmsMartin @ 2022-02-04 13:19:27

@limuwen666 直接不打分号, CE


by xzx_thanks_star @ 2022-02-04 13:21:00

或者输出样例,WA


by Butterfly__qwq @ 2022-02-04 13:28:45

或者输出加个while(1)

会遇到OLE奇观


| 下一页