int Fibonacci_recursive(int n) {
if (n == 0) {
return 0;
}
else {
return n + Fibonacci_recursive(n - 1);
}
}
int Fibonacci_non_recursive(int n) {
int sum = 0;
while (n != 0) {
sum += n;
n--;
}
return sum;
}
int Combinations(int n, int k)
{
if(k == 1)
return n;
else if(n == k)
return 1;
else
return (Combinations(n-1,k)+ Combinations(n-1.k-1));
}
int Power(int x, int n)
{
if (n==0)
return 1;
else
return (x * Power(x, n-1));
}
- 재귀를 사용한 거꾸로 출력(RevPrint)
- 재귀호출이 아니었다면 반복문을 통해 매번 끝까지 갔다가, 끝 전까지 갔다가...이거를 반복해야함
#include <iostream>
template <class ItemType>
struct NodeType
{
int info;
NodeType<ItemType>* next;
};
template <class ItemType>
void RevPrint(NodeType<ItemType>* listPtr)
{
using namespace std;
if (listPtr != NULL)
{
RevPrint(listPtr->next);
cout << listPtr->info << endl;
}
}
template<class ItemType>
bool BinarySearch (ItemType info[], ItemType item,
int fromLocation, int toLocation)
{
if (fromLocation > toLocation)
return false;
else
{
int midPoint = (fromLocation + toLocation) / 2;
if (item < info[midPoint])
return BinarySearch(info, item, fromLocation, midPoint-1);
else if (item == info[midPoint])
return true;
else
return BinarySearch(info, item, midPoint + 1, toLocation);
}
}
template<class ItemType>
void Insert(NodeType<ItemType>*& location, ItemType item)
{
if (location == NULL || item < location->info)
{
NodeType<ItemType>* tempPtr = location;
location = new NodeType<ItemType>;
location >info = item;
location->next = tempPtr;
}
else Insert(location->next, item);
}
template<class ItemType>
void Delete(NodeType<ItemType>*& listPtr, ItemType item)
{
if (item == listPtr->info)
{
NodeType<ItemType>* tempPtr = listPtr;
listPtr = listPtr->next;
delete tempPtr;
}
else
Delete(listPtr->next, item);
}