有一个分数序列,2/1, 3/2, 5/3, 8/5, 13/8, 21/13……先观察数列规律,再求出数列的前n项之和,用double型变量存储结果,哪里有问题?#include<stdio.h>main(){int a=1,b=2,n,i=1,r;double sum=0,x;printf("Please input an integer:");scanf("%d",&n);while(i<=n){ x=b/a;sum=sum+x;r=a+b;a=b;b=r;i++;}printf("The result is %lf",sum);return 0;}
1 回答
MYYA
TA贡献1868条经验 获得超4个赞
#include<stdio.h> main() { int a=1,n,i=1,r; double sum=0,b=2,x; printf ( "Please input an integer:" ); |
scanf ( "%d" ,&n); while (i<=n) { x=b/a; //这里你原来是2/1,结果是2,是int型,所以我把b也换了。 sum=sum+x; r=a+b; a=b; b=r; i++; } printf ( "The result is %lf" ,sum); return 0; } |
- 1 回答
- 0 关注
- 125 浏览
添加回答
举报
0/150
提交
取消