C语言求助

P1035 [NOIP2002 普及组] 级数求和

zhaoanqiEtayo @ 2023-07-27 16:06:05

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int k;
    double s,n;
    scanf("%d",&k);
    s=0;
    for(n=1;;n++)
    {
        s=s+1/n;
        if(s>k)
        {
            break;
        }
    }
    printf("%lf",n);
    return 0;
}

by FurippuWRY @ 2023-07-27 16:15:58

@zhaoanqiEtayo
can can word

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

by ___Yang__ @ 2023-07-27 16:18:37

@zhaoanqiEtayo

#include<stdio.h>
#include<stdlib.h>
int main()
{
    int k,n;
    double s,i=1;
    scanf("%d",&k);
    s=0;
    for(n=1;;n++,i++)
    {
        s+=1/i;
        if(s>k)
        {
            break;
        }
    }
    printf("%d",n);//要用int类型输出哦
    return 0;
}

by Kotori_Kawaii @ 2023-08-03 18:21:44

不会灵活使用for语句的萌新QAQ


    #include<stdio.h>

    int main() {

        int k;
        scanf("%d", &k);

        double i = 0;
        double n = 0;

        do {
            ++n;
            double a = 1/n;
            i = i + a;
        } while (i<= k);

        printf("%.0f",n);

        return 0;
    }

by sunwukong @ 2023-08-27 22:55:54

#include<stdio.h>
#include<stdlib.h>
int main()
{

    int k,n;

    double s;   //n要求输出为整数所以不能为double类型
    scanf("%d",&k);
    s=0;
    for(n=1;;n++)
    {
        s=s+1.0/n;  //主意必须是1.0,否则会四舍五入为整数,n已经变为整数
        if(s>k)
        {
            break;
        }
    }
    printf("%d",n);
    return 0;
}

|