四十分求解!

P1035 [NOIP2002 普及组] 级数求和

lihaoran2012 @ 2023-08-30 11:54:38

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

by SY_Nightmare @ 2023-08-30 12:15:20

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

@lihaoran2012 这样


by Miyamizu_Mitsuha @ 2023-08-30 12:17:04

判断s>=k,然后建议全部改成double型


by Miyamizu_Mitsuha @ 2023-08-30 12:17:38

不对,是s>k,说错了


by Andy_hpy @ 2023-09-13 15:10:16

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

|