0分c++求助><……

P1035 [NOIP2002 普及组] 级数求和

NNFeNN @ 2023-03-12 20:59:24

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

by _Waldeinsamkeit_ @ 2023-03-12 21:42:17

所有变量附double

函数for的终止条件错了,可改成while

函数return的变量不对

k初值为0

main里的while纯属多余

fen有返回值,没接收

修改后代码:

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

by NNFeNN @ 2023-03-14 21:15:20

!!!感谢神犇


|