全WA了QAQ

P1035 [NOIP2002 普及组] 级数求和

___wa___ @ 2022-10-02 21:06:10

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a;
    cin>>a;
    double b=0;
    int i=1;
    while(b<a)
    {
        b+=1.0/i;
        i++;
    }
    cout<<i;
    return 0;
}

哪里错了...


by yingbowen @ 2022-10-02 21:12:20

@wa i初始值改为0,然后交换while循环里的两行代码


by Light_az @ 2022-10-02 21:12:50

#include<bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    int a;
    cin>>a;
    double b=0;
    int i=1;
    while(b<a)
    {
        b+=1.0/i;
        i++;
    }
    cout<<i-1;//结果会多加
    return 0;
}

by ___wa___ @ 2022-10-02 21:14:27

谢谢dalao


|