WA

P1035 [NOIP2002 普及组] 级数求和

ammdyc @ 2022-10-09 20:51:21


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

by shihaoran @ 2022-10-13 12:31:05

找到问题了


by shihaoran @ 2022-10-13 12:31:56

把x+=1.0/y这句和y++调换一下位置


by shihaoran @ 2022-10-13 12:35:14

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

|