why!

P1035 [NOIP2002 普及组] 级数求和

nobody_here @ 2024-01-29 21:44:07

#include<iostream>
using namespace std;
double jh(int n){
    double num=0;
    for(int i=1;i<=n;i++){
        num+=1/i;
    }
    return num;
}
int main(){
    double a;
    cin>>a;
    for(int i=1;i<=a;i++){
        if(jh(i)>a){
            cout<<i;
            return 0;
        }
    }
    return 0;
}

by Lijunzhuo @ 2024-01-29 21:45:16

1.0


by Lijunzhuo @ 2024-01-29 21:45:51

1.0/i 要转double


by Lijunzhuo @ 2024-01-29 21:46:37

#include<iostream>
using namespace std;
double jh(int n){
    double num=0;
    for(int i=1;i<=n;i++){
        num+=1/i;
    }
    return num;
}
int main(){
    double a;
    cin>>a;
    for(int i=1;i<=a;i++){
        if(jh(i)>a){
            cout<<i;
            return 0;
        }
    }
    return 0;
}


by Lijunzhuo @ 2024-01-29 21:47:37

#include<iostream>
using namespace std;
double jh(int n){
    double num=0;
    for(int i=1;i<=n;i++){
        num+=1.0/i;
    }
    return num;
}
int main(){
    double a;
    cin>>a;
    for(int i=1;i<=a;i++){
        if(jh(i)>a){
            cout<<i;
            return 0;
        }
    }
    return 0;
}

发错了qwq


by kotoamatsukami @ 2024-01-31 11:41:37

@nobody_here 为什么是两个循环? 其实不需要主程序中的循环i


by nobody_here @ 2024-02-02 21:33:29

谢谢大佬们,AC了


|