I come, I see, I conquer

                    —Gaius Julius Caesar

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 :: 管理 ::
#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 == 0return 0;
    
else if (n == 1return 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 == 0return 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"3549, gcd(3549));
    printf(
"gcd_iter(%d, %d) = %d\n"3549, gcd_iter(3549));

    hanoi(
3'x''y''z');
    
int x[] = { 36791243225};
    printf(
"binary_search: %d's index is %d\n"12, bsearch_iter(x, 1206));
    printf(
"binary_search: %d's index is %d\n"13, bsearch_iter(x, 1306));
}
posted on 2011-04-17 14:14  jcsu  阅读(312)  评论(0)    收藏  举报