求助,为啥只有最后一个是绿的

P1035 [NOIP2002 普及组] 级数求和

chhhhhhke @ 2024-10-11 18:35:16

#include<stdio.h>
int main()
{
    int n=1;
    double sum=0.0;
    int k;
    scanf("%d",&k);
   for(sum=0.0;sum<k;n++)
   {
       sum=sum+1.0/n;
   }
    printf("%d\n",n);
    return 0;
}

by NewbieZZZ @ 2024-10-11 18:40:23

将最后一行改为 printf("%d\n", n-1);?它会多算一次


by luogu_00 @ 2024-10-12 17:10:27

AC代码:

#include <stdio.h>
int main()
{
    int k,i=1;
    scanf("%d",&k);
    double sum=0.0;
    while(sum <= k)
    {
        sum+=1.0/i;
        i++;
    }
    printf("%d",n-1);
    return 0;
}

|