大佬帮忙看看为什么运行不了

P1035 [NOIP2002 普及组] 级数求和

jgdlkfjg @ 2023-11-01 23:25:57

大佬帮忙看看为什么运行不了

 #include<stdio.h>
#include<math.h>

double s(int n){
    int i,s;
    s=0.0;
    for(i=1;i<=n;i++)
    {
        s=s+(1/i);
    }
    return s;
}

int main()
{
    int a,k;
    k=0;
    scanf("%d",k);
    a=1;
    while(s(a)<k)
    {
        a=a+1;
    }
    printf("%d",a);

}

by Ehuo_ovo @ 2023-11-02 00:10:23

子函数第一行 改成 double s @jgdlkfjg


by jgdlkfjg @ 2023-11-02 07:30:56

@Ehuo_ovo 还是不行,改完还是运行不了?

#include<stdio.h>
#include<math.h>

double s(double n){
    int i;
    double s;
    s=0.0;
    for(i=1;i<=n;i++)
    {
        s=s+(1/i);
    }
    return s;
}

int main()
{
    double a,k;
    k=0.0;
    scanf("%f",&k);
    a=1.0;
    while(s(a)<k)
    {
        a=a+1;
    }
    printf("%f",a);
    return 0;

}

by LZMqueueNB @ 2023-11-02 07:55:12

@jgdlkfjg scanf("%d",&k)

k前面要加 "&" ,不然是一个不指针


by Secret_Ingredient @ 2023-11-02 08:03:21

@jgdlkfjg s函数里改成1.0/i


by jgdlkfjg @ 2023-11-02 15:32:38

解决了,多谢


|