本蒟蒻真的不知道哪错了

P1035 [NOIP2002 普及组] 级数求和

mc_xiexie @ 2023-10-04 15:25:23

代码

#include<bits/stdc++.h>
using namespace std; 
int main()
{
    long long s,n,k,q;
    cin>>k;
    for (n=1;n<=k;n++)
    {
        s=s+1/n;
        if(s>k)
        {
             cout<<n;
        }
    }
}

by pengrongxuan @ 2023-10-04 15:26:31

1/k是整除,要1.0/k


by _buzhidao_ @ 2023-10-04 15:26:52

@mc_xiexie long long改成double


by 13245zy @ 2023-10-04 15:27:04

#include<iostream>
using namespace std;
int main(){
    int k;
    double sum=1;
    cin>>k;
    for(int i=2;;i++){
        sum+=1.0/i;
        if(sum>k){
            cout<<i<<endl;
            return 0;
        }
    }
    return 0;
}

by _buzhidao_ @ 2023-10-04 15:27:43

@mc_xiexie 同时改成1.0/k


by 13245zy @ 2023-10-04 15:28:31

觉得有用就赏个关注呗,(我很不要脸的说)。


by _buzhidao_ @ 2023-10-04 15:28:36

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

by mc_xiexie @ 2023-10-04 15:30:09

谢谢了,已关


|