int f(int N)
{
if (N==0) return 0;
else if (N==1) return 1;
else return f(n-1)+f(n-2);
}
例如下面这样来实现阶乘
int fact(int n)
{
int num=1;
stack S;
L:
if(n>0)
{
S.push(n);
n=n-1;
goto L;
M:
num*=S.top();
S.pop();
}
if(!S.empty())
{
goto M;
}
return num;
}
// Q755200.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdio.h>
int stack[1000];
int curr = 0;
void push(int n)
{
if (curr >= 1000) printf("stack overflow!!");
stack[curr++] = n;
}
int pop()
{
if (curr < 0) printf("stack empty!!!");
return stack[--curr];
}
int f(int n1)
{
int a;
int b;
push(0);
push(n1);
push(0);
callstart:
int pos = pop();
int n = pop();
if (n == 1 || n == 2)
{
push(1);
if (pos == 1) goto endcall1;
if (pos == 2) goto endcall2;
if (pos == 0) return 1;
}
push(n);
push(a);
push(b);
push(pos);
push(n - 1);
push(1);
goto callstart;
endcall1:
a = pop();
pos = pop();
b = pop();
pop();
n = pop();
push(n);
push(a);
push(b);
push(pos);
push(n - 2);
push(2);
goto callstart;
endcall2:
b = pop();
pos = pop();
pop();
a = pop();
n = pop();
push(a + b);
if (pos == 1) goto endcall1;
if (pos == 2) goto endcall2;
if (pos == 0) return a + b;
}
int main()
{
for (int i = 0; i < 10; i++)
printf("%d ", f(i + 1));
return 0;
}