经典算法大全


  1. 河内之塔(Towers of Hanoi)
#include <stdio.h>

// 汉诺塔核心代码 递归
void hanoi_recursive(int n, char A,char B,char C){
    if (n==1)
    {
        printf("Move sheet %d from %c to %c",n,A,C);
    }
    else{
        hanoi_recursive(n-1,A,C,B);
        printf("Move sheet %d from %c to %c \n",n,A,C);
        hanoi_recursive(n-1,B,A,C);
    }    
}

// 输入柱子数量,返回需要执行的步骤
int TowerOfHanoi(){
    int n;
    printf("请输入整数:");
    scanf("%d",&n);
    hanoi_recursive(n,'A','B','C');
    return 0;
}
  1. 费氏数列
#include <stdio.h>
#include <stdlib.h>

#define N0 20

// 费氏数列
int Fibonacci()
{
    int Fib[N0] = {0};
    int i;

    Fib[0] = 0;
    Fib[1] = 1;

    for (i = 2; i < N0; i++)
    {
        Fib[i] = Fib[i - 1] + Fib[i - 2];
    }

    for (i = 0; i < N0; i++)
    {
        printf("%d ", Fib[i]);
        printf("\n");
    }

    return 0;
}

文章作者: rainbow
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 rainbow !
评论
  目录