-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path250.c
81 lines (69 loc) · 1.43 KB
/
250.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// http://www.bjfuacm.com/problem/250/
// https://codegolf.stackexchange.com/questions/40141/the-ackermann-function
/**********************
* ACKERMANN FUNCTION *
* NON-RECURSIVE *
* IMPLEMENTATION *
**********************/
/* AC: CPP
#include <iostream>
#include <stack>
using namespace std;
int ack(int m, int n) {
stack<int> s;
s.push(m);
while (!s.empty()) {
m = s.top();
s.pop();
if (m == 0) {
n = n + 1;
} else if (n == 0) {
n = 1;
s.push(m - 1);
} else {
n = n - 1;
s.push(m - 1);
s.push(m);
}
}
return n;
}
int main() {
int m, n;
while (cin >> m >> n) {
if (m == 0 && n == 0) break;
cout << ack(m, n) << endl;
}
}
*/
#include <stdio.h>
#include <stdlib.h>
#define MAXN 10000
int Ack(int m, int n) {
int* stack = (int *)calloc(MAXN, sizeof(int));
int i = 0;
stack[++i] = m;
while (i != 0) {
m = stack[i--];
if (m == 0) {
n = n + 1;
} else if (n == 0) {
n = 1;
stack[++i] = m - 1;
} else {
n = n - 1;
stack[++i] = m - 1;
stack[++i] = m;
}
}
free(stack);
return n;
}
int main() {
int m, n;
while (1) {
scanf("%d %d", &m, &n);
if (m == 0 && n == 0) break;
printf("%d\n", Ack(m, n));
}
}