// C#
// recur version
int AddRecur(int val)
if (val > 0)
return val + AddRecur(val - 1);
return 0;
// iter version
int AddIter(int val)
var ret = 0;
for (int i = 1; i <= val; ++i)
ret += i;
return ret;
}
// C#
// recur version
long FibonacciRecur(long index)
if (index <= 1)
return index;
return FibonacciRecur(index - 1) + FibonacciRecur(index - 2);
// iter version
long FibonacciIter(long index)
if (index <= 1)
return index;
long pre = 0;
long cur = 1;
long next = 0;
for (long i = 2; i <= index; ++i)
// calc next
next = pre + cur;
// update pre and cur
pre = cur;
cur = next;
return next;
}
// C#
Dictionary<long, long> s_buffer = new Dictionary<long, long>();
long FibonacciRecur(long index)
if (index <= 1)
return index;
long pre = 0;
if (!s_buffer.TryGetValue(index - 1, out pre))
pre = FibonacciRecur(index - 1);
s_buffer[index - 1] = pre;
long cur = 0;
if (!s_buffer.TryGetValue(index - 2, out cur))
cur = FibonacciRecur(index - 2);
s_buffer[index - 2] = cur;
return pre + cur;
}
// C#
public class Node
string m_name;
List<Node> m_children = new List<Node>();
public Node(string name, params Node[] children)
m_name = name;
m_children.AddRange(children);
public string GetName()
return m_name;
public int GetChildCount()
return m_children.Count;
public Node GetChild(int index)
if (index >= 0 && index < m_children.Count)
return m_children[index];
return null;
}