用c语言实现一个数列的求和问题。菜鸟上路,高手指点
/************************************************
problem: 1+1=2
2+1=3
3+2=5
5+3=8
8+5=13......
*************************************************/
// my code:
#include <stdlib.h>
#include <stdio.h>
int fib(int);
int main(void)
{
int temp,x;
scanf("%d",&temp);
x=fib(temp);
printf("%d\n",x);
return 0;
}
int fib(int input)
{
int x=1,y=2,i=1;
if (input == 1)
{
return y;
}
else
{
int temp=0;
for (; i<input; i++)
{
temp = x+y;
x = y;
y = temp;
}
}
return y;
}