0分

P1035 [NOIP2002 普及组] 级数求和

NachoDoritosLOL @ 2024-08-25 08:54:18

#include<bits/stdc++.h>
using namespace std;
int main(){
    int k=0,n;
    float sn=0.0;
    cin>>k;
    for(n=1;sn>k*1.0;n++){
        sn+=1/n;
    }
    cout<<n;
    return 0;
}

Why 我 wrong 啊?! Every 大佬 help 我!


by lce11451410086 @ 2024-08-26 14:31:32

ac代码


#include<bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin>>n;
    double s=1;
    for(int i=2;;i++){
        s+=1.0/i;
        if(s>n){
            cout<<i;
            break;
        }
    }
    return 0;
}

by lce11451410086 @ 2024-08-26 14:34:28

@NachoDoritosLOL


by lce11451410086 @ 2024-08-26 14:40:08

用你代码改的


#include<bits/stdc++.h>
using namespace std;
int main(){
    int k=0;
    double n;
    double sn=1.0;
    cin>>k;
    for(n=2;;n++){
        sn+=1.0/n;
        if(sn>k){
            break;
        }
    }
    cout<<n;
    return 0;
}

by lce11451410086 @ 2024-08-26 14:44:29

1.float改成double
2.应该改成1.0/n(或者把n改成double)
3.条件单独判断
@NachoDoritosLOL


by lce11451410086 @ 2024-08-26 14:45:43

qiu guan(手动狗头)


|