Skip to content

Instantly share code, notes, and snippets.

@pineapplehunter
Created October 2, 2020 07:44
Show Gist options
  • Select an option

  • Save pineapplehunter/7a977553b8e770ca3624c1a8048f8a48 to your computer and use it in GitHub Desktop.

Select an option

Save pineapplehunter/7a977553b8e770ca3624c1a8048f8a48 to your computer and use it in GitHub Desktop.
アルゴリズム論の自作スタックを使ってた例のgotoをなくしたやつ
#include <stdio.h>
int fib2 (int);
int main () { printf ("%d\n", fib2 (3)); }
int fib2 (int n) {
int stack[100], sp = 0, r = 0, flg = 0;
while (1) {
if (n <= 1) {
r = 1;
} else {
if (flg == 0) {
stack[sp++] = 0; // push(0)
stack[sp++] = n; // push(n)
n--;
continue;
} else if (flg == 1) {
stack[sp++] = r; // push(r)
stack[sp++] = 1; // push(1)
stack[sp++] = n; // push(n)
n -= 2;
flg = 0;
continue;
} else {
r += stack[--sp]; // pop()
flg = 0;
}
}
if (sp == 0)
return r;
n = stack[--sp]; // pop()
if (stack[--sp] == 0) // pop()
flg = 1;
else
flg = 2;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment