程序分析:抓住分子与分母的变化规律:
分子a:1,2,3,5,8,13,21,34,55,89,144...
分母b:2,3,5,8,13,21,34,55,89,144,233...
分母b把数赋给了分母a,同时自己与分母的和(a+b)变成新分子赋给分母b。
代码:
#include<stdio.h> int main() { int a = 1, b = 2, i, j, n; float s = 0.0; printf("请输入项数:\n"); scanf_s("%d", &n); for (i = 1; i <= n; i = i + 1) { s = s + 0.1 * a / b; j = a; a = b; b = b + j; } printf("%f", s); }