#include <stdio.h>
/************************************************************************/
/* */
/* 自然数的阶乘 (factorial of natural number) */
/* fact(n) = 1 if n = 0 */
/* fact(n) = n * fact(n - 1) if n > 0 */
/* */
/************************************************************************/
int factorial(int n) /* recursive */
{
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int factorial_iter(int n) /* iterative */
{
int total = 1;
while (n > 0)
{
total *= n;
n--;
}
return total;
}
/************************************************************************/
/* */
/* 斐波那契数 (Fibonacci) */
/* fib(n) = 0 if n = 0 */
/* fib(n) = 1 if n = 1 */
/* fib(n) = fib(n - 1) + fib(n - 2) if n >= 2 */
/* */
/************************************************************************/
int fib(int n) /* recursive */
{
if (n == 0) return 0;
else if (n == 1) return 1;
else return fib(n - 1) + fib(n - 2);
}
/************************************************************************/
/* */
/* 最大公约数 (Greatest common divisor) */
/* gcd(x, y) = x if y = 0 */
/* gcd(x, y) = gcd(y, remainder(x, y)) if x >= y and y > 0 */
/* */
/* input: integer x, integer y such that x >= y and y >= 0 */
/* */
/************************************************************************/
int gcd(int x, int y) /* recursive */
{
if (y == 0) return x;
else return gcd(y, x % y);
}
int gcd_iter(int x, int y) /* iterative */
{
int remainder = 0;
while (y > 0)
{
remainder = x % y;
x = y;
y = remainder;
}
return x;
}
/************************************************************************/
/* */
/* 汉诺塔 (Towers of Hanoi) */
/* */
/************************************************************************/
#define move(x, n, z) printf("%i. Move disk %i from %c to %c\n", ++c, n, x, z);
int c = 0;
void hanoi(int n, char x, char y, char z)
{
if (n == 1)
{
move(x, 1, z);
}
else
{
hanoi(n - 1, x, z, y);
move(x, n, z);
hanoi(n - 1, y, x, z);
}
}
/************************************************************************/
/* */
/* 二分查找 (Binary Search) */
/* */
/************************************************************************/
int bsearch_iter(int *data, int toFind, int start, int end)
{
int mid = start + (end - start)/2;
if (start > end)
return -1;
else if (data[mid] == toFind)
return mid;
else if (data[mid] > toFind)
return bsearch_iter(data, toFind, start, mid-1);
else
return bsearch_iter(data, toFind, mid+1, end);
}
void main()
{
printf("factorial(%d) = %d\n", 5, factorial(5));
printf("factorial_iter(%d) = %d\n", 5, factorial_iter(5));
printf("fibonacci(%d) = %d\n", 10, fib(10));
printf("gcd(%d, %d) = %d\n", 35, 49, gcd(35, 49));
printf("gcd_iter(%d, %d) = %d\n", 35, 49, gcd_iter(35, 49));
hanoi(3, 'x', 'y', 'z');
int x[] = { 3, 6, 7, 9, 12, 43, 225};
printf("binary_search: %d's index is %d\n", 12, bsearch_iter(x, 12, 0, 6));
printf("binary_search: %d's index is %d\n", 13, bsearch_iter(x, 13, 0, 6));
}
/************************************************************************/
/* */
/* 自然数的阶乘 (factorial of natural number) */
/* fact(n) = 1 if n = 0 */
/* fact(n) = n * fact(n - 1) if n > 0 */
/* */
/************************************************************************/
int factorial(int n) /* recursive */
{
if (n == 0)
return 1;
else
return n * factorial(n - 1);
}
int factorial_iter(int n) /* iterative */
{
int total = 1;
while (n > 0)
{
total *= n;
n--;
}
return total;
}
/************************************************************************/
/* */
/* 斐波那契数 (Fibonacci) */
/* fib(n) = 0 if n = 0 */
/* fib(n) = 1 if n = 1 */
/* fib(n) = fib(n - 1) + fib(n - 2) if n >= 2 */
/* */
/************************************************************************/
int fib(int n) /* recursive */
{
if (n == 0) return 0;
else if (n == 1) return 1;
else return fib(n - 1) + fib(n - 2);
}
/************************************************************************/
/* */
/* 最大公约数 (Greatest common divisor) */
/* gcd(x, y) = x if y = 0 */
/* gcd(x, y) = gcd(y, remainder(x, y)) if x >= y and y > 0 */
/* */
/* input: integer x, integer y such that x >= y and y >= 0 */
/* */
/************************************************************************/
int gcd(int x, int y) /* recursive */
{
if (y == 0) return x;
else return gcd(y, x % y);
}
int gcd_iter(int x, int y) /* iterative */
{
int remainder = 0;
while (y > 0)
{
remainder = x % y;
x = y;
y = remainder;
}
return x;
}
/************************************************************************/
/* */
/* 汉诺塔 (Towers of Hanoi) */
/* */
/************************************************************************/
#define move(x, n, z) printf("%i. Move disk %i from %c to %c\n", ++c, n, x, z);
int c = 0;
void hanoi(int n, char x, char y, char z)
{
if (n == 1)
{
move(x, 1, z);
}
else
{
hanoi(n - 1, x, z, y);
move(x, n, z);
hanoi(n - 1, y, x, z);
}
}
/************************************************************************/
/* */
/* 二分查找 (Binary Search) */
/* */
/************************************************************************/
int bsearch_iter(int *data, int toFind, int start, int end)
{
int mid = start + (end - start)/2;
if (start > end)
return -1;
else if (data[mid] == toFind)
return mid;
else if (data[mid] > toFind)
return bsearch_iter(data, toFind, start, mid-1);
else
return bsearch_iter(data, toFind, mid+1, end);
}
void main()
{
printf("factorial(%d) = %d\n", 5, factorial(5));
printf("factorial_iter(%d) = %d\n", 5, factorial_iter(5));
printf("fibonacci(%d) = %d\n", 10, fib(10));
printf("gcd(%d, %d) = %d\n", 35, 49, gcd(35, 49));
printf("gcd_iter(%d, %d) = %d\n", 35, 49, gcd_iter(35, 49));
hanoi(3, 'x', 'y', 'z');
int x[] = { 3, 6, 7, 9, 12, 43, 225};
printf("binary_search: %d's index is %d\n", 12, bsearch_iter(x, 12, 0, 6));
printf("binary_search: %d's index is %d\n", 13, bsearch_iter(x, 13, 0, 6));
}

浙公网安备 33010602011771号