求分數序列
2/1 +3/2+5/3+8/5+13/8+21/13...
前20項之和。
我這樣的代碼為什麼不對啊?輸出是21.00000000 答案輸出是32.6602607986
#include <stdio.h>
int main()
{
int fenzi(int n);
int fenmu(int m);
int i;
float s=0,tem;
for(i=1;i<=20;i++)
{
tem=fenzi(i)/fenmu(i);
s=s+tem;
}
printf("%f",s);
return 0;
}
int fenzi(int n)
{
if(n==1) return 2;
else if(n==2) return 3;
else return (fenzi(n-2)+fenzi(n-1));
}
int fenmu(int m)
{
if(m==1) return 1;
else if(m==2) return 2;
else return (fenmu(m-2)+fenmu(m-1));
}
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
int fenzi(int n);
int fenmu(int m);
int main()
{
int i;
float s = 0, tem;
for (i = 1; i <= 20; i++)
{
tem = (double)fenzi(i) / (double)fenmu(i);
s = s + tem;
}
printf("%f", s);
return 0;
}
int fenzi(int n)
{
if (n == 1) return 2;
else if (n == 2) return 3;
else return (fenzi(n - 2) + fenzi(n - 1));
}
int fenmu(int m)
{
if (m == 1) return 1;
else if (m == 2) return 2;
else return (fenmu(m - 2) + fenmu(m - 1));
}