一直超时,我要疯啦!求大佬出手救救我(另附一大佬的解,小白实在不懂区别何在)

P1035 [NOIP2002 普及组] 级数求和

GoatLeo @ 2021-08-15 17:12:25

include <stdio.h>

include <stdlib.h>

int main() { int k,n; double Sn=0.0; for(n=1;Sn<=k;n++) { Sn+=1/n;

}
printf("%d",n);
return 0;

}```c

include<stdio.h>

int main(){ int k,n; double sn=0.0; scanf("%d",&k); for(n=1;;n++){ sn+=1.0/n; if(sn>k*1.0) break; } printf("%d",n); return 0; }


by xyuwwwm5 @ 2021-08-15 17:13:09

望丰展?使Md


by xyuwwwm5 @ 2021-08-15 17:13:52

您应该用代码框框全


by wzmzmhk @ 2021-08-15 17:15:55

他的代码

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int k, n;
    double Sn = 0.0;
    for (n = 1; Sn <= k; n++)
    {
        Sn += 1 / n;
    }
    printf("%d", n);
    return 0;
}

大佬的代码

#include<stdio.h>

int main()
{
    int k, n;
    double sn = 0.0;
    scanf("%d", &k);
    for (n = 1;; n++) {
        sn += 1.0 / n;
        if (sn > k * 1.0)
            break;
    }
    printf("%d", n);
    return 0;
}

by 添哥 @ 2021-08-15 17:16:47

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

重新排版了一下,建议学习一下Markdown


by _短笛无腔_ @ 2021-08-15 17:16:49

@GoatLeo 循环条件改一下


by wzmzmhk @ 2021-08-15 17:17:02

@GoatLeo 明显的错误:你的判断结束循环条件错误


by GoatLeo @ 2021-08-15 20:16:42

@短笛无腔 我看不出区别啊,怎么改呢


by GoatLeo @ 2021-08-15 20:50:14

@短笛无腔 啊哈,解决了,谢谢大佬


by rpmcppAFOed @ 2021-08-16 08:34:11

@GoatLeo 望丰展,使md


|