czh1 @ 2023-11-17 21:24:16
#include<stdio.h>
int main() {
int k,n=1;
double s=0;
scanf("%d",&k);
while(s<=k){
s+=1/n*1.0;
n++;
}
printf("%d",n);
return 0;
}
by yaominghao2007 @ 2023-11-17 21:39:42
#include<stdio.h>
int main() {
int k,n=1;
double s=0;
scanf("%d",&k);
while(s<=k){
s+=1/(n*1.0);
n++;
}
printf("%d",n-1);
return 0;
}
@czh1 由于你的代码中n为int类型,所以1/n永远为0(把1.0乘到n上就好了),还有,要输出n-1
by _d_h_f @ 2023-11-17 21:41:02
第七行建议改为 s+=(double)1/(double)n*1.0
第十行建议改为 printf("%d\n",n-1)
by czh1 @ 2023-11-19 14:03:47
@_d_h_f @yaominghao2007 谢谢