请问为啥我这个出不来结果呢?

P1035 [NOIP2002 普及组] 级数求和

guerdan @ 2018-11-19 21:11:14

include<stdio.h>

int main() {

float x=1;
float sum=0;
int a;
 scanf("%d",&a);

while(sum<a) { sum=sum+1/x; x++;} printf("%d",x); return 0; }


by presucc @ 2018-11-19 21:13:46

include<stdio.h>

int main() {

float x=1;
float sum=0;
int a;
 scanf("%d",&a);
while(sum<a) { sum=sum+1/x; x++;} printf("%d",x); return 0; }

by presucc @ 2018-11-19 21:15:41

@guerdan 输出浮点数不能用%d


by presucc @ 2018-11-19 21:16:41

@guerdan 如果是要输出整数的话就来个强制类型转换


by guerdan @ 2018-11-19 21:18:18

@龙月语 哈,知道了,感谢


by guerdan @ 2018-11-19 21:28:56

@龙月语 #include<stdio.h> int main() {

int x=1; double sum=0; int a; scanf("%d",&a); while(sum<a) { sum=sum+1.0/x; x++;} printf("%d",x); return 0; }


by guerdan @ 2018-11-19 21:29:20

@龙月语 这个在我自己的编译器上是可以有结果的,但是是0分啊


|