为什么前几个可以ac最后一个过不了嘞

P1035 [NOIP2002 普及组] 级数求和

Sylvan_Asher @ 2024-09-20 15:30:08

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

by maimai_slide_madman @ 2024-09-20 15:35:38

计算精度问题(应该?


by Yxy7952 @ 2024-09-20 15:37:56

@wxm18250239335

求关

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

by Yxy7952 @ 2024-09-20 15:41:50

@maimai_slide_madman

额,看一下题其实就行了


by maimai_slide_madman @ 2024-09-20 15:42:34

@Yxy7952 谢谢(^^)


by AA_rui @ 2024-09-28 12:44:50

@Yxy7952 为什么我的最后一个也过不了


by wangshengchen @ 2024-09-28 14:36:33

@AA_rui 我的代码

#include<iostream>
#include<algorithm>
#include<cmath>
using namespace std;
int main()
{
    #define int long long
    #define double long double
    int k, n = 0; 
    cin >> k; 
    double Sn = 0; 
    for (; Sn <= k; ++n, Sn += 1.0 / n); 
    cout << n;
    #undef int
    return 0; 
}

by wangshengchen @ 2024-09-28 14:37:29

@AA_rui 或

#include<iostream>
using namespace std;
int main()
{
    int k, n = 0; 
    cin >> k; 
    double Sn = 0; 
    for (; Sn <= k; ++n, Sn += 1.0 / n); 
    cout << n;
    return 0; 
}

by wangshengchen @ 2024-09-28 14:38:50

@AA_rui 求关


by Yxy7952 @ 2024-09-29 14:09:53

@AA_rui

关注我,发代码


by SunLaoC @ 2024-09-30 14:45:32

#include<iostream>
using namespace std;
int main(){
    int i, k,n=0;
    double S=0;

    cin >> k;
    for ( i = 1; ; i++)
    {
        n = i;
        S = S +1.0 / i;
        if (S>k)
        {
            break;  
        }
    }
    printf("%d", n);
    return 0;
}

| 下一页