不明白哪里错了

P1035 [NOIP2002 普及组] 级数求和

wei060925 @ 2024-11-25 10:41:01

#include<stdio.h>
int main()
{
    int n,a,sum,k;
    a=1/n;
    scanf("%d",k);
    for(k=1;k<=15;k++)
    {
        for(n=1;n<10000;n++)
        {
            sum+=a;
            if(sum>k)
            break;
        }
    }
    printf("%d ",n);
    return 0;
}

by Xx0okoukio0xX @ 2024-11-25 10:45:37

sum是浮点数,得用double

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

by Xx0okoukio0xX @ 2024-11-25 10:46:05

@wei060925


|