王岐9.6-递归:求最大公因数;N皇后-多米诺矩阵-西工大算法

  感觉王岐上课讲的还行。

  求最大公因数:

#include <stdio.h>
#include <stdlib.h>

void GCD (int a, int b);

int main()
{
    int a, b;
    scanf ("%d%d", &a, &b);
    GCD (a, b);
    return 0;
}

void GCD (int a, int b)
{
    if (b == 0)
    {
        printf ("%d\n", a);
    }
    else
    {
        GCD (b, a%b);
    }
}

  N皇后:

  

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void findQueenPosition (int layer);
int isInTheRightPosition (int layer, int j);
void outPut ();

int pos[3000];
int n, no = 1;

int main()
{
    scanf ("%d", &n);
    findQueenPosition (1);
    return 0;
}

void findQueenPosition (int layer)
{
    int j;
    if (layer == n + 1)
    {
        outPut ();
    }
    else
    {
        for (j = 1; j <= n; j++)
        {
            if (isInTheRightPosition (layer, j))
            {
                pos[layer] = j;
                findQueenPosition(layer + 1);
            }
        }
    }
}

int isInTheRightPosition (int layer, int j)
{
    int i;
    for (i = 1; i < layer; i++)
    {
        if (pos[i] == j || abs (j - pos[i]) == abs (layer - i))
        {
            return 0;
        }
    }
    return 1;
}

void outPut ()
{
    int i, j;
    printf ("NO : %d\n", no++);
    for (i = 1; i <= n; i++)
    {
        for (j =1; j <= n; j++)
        {
            if (pos[i] == j)
            {
                printf ("@");
            }
            else
            {
                printf (".");
            }
        }
        printf ("\n");
    }
    printf ("\n");
}













多米诺矩阵:

这个我就写了一下递归枚举排列方法

#include <stdio.h>
#include <stdlib.h>

struct position
{
    int x;
    int y;
};

struct position getNextPosition (struct position prePos);
void findBoardType (struct position prePos);
int placeADomino (struct position pos, int type);
void delateADomino (struct position pos, int type);
void outPut ();

struct position boundry;
int board[200][200];
int typeNo = 1;
int no = 1;

int main()
{
    scanf ("%d%d", &boundry.x, &boundry.y);
    struct position pos = {1, 1};
    findBoardType (pos);
    return 0;
}

struct position getNextPosition (struct position prePos)
{
    int i, j;
    struct position pos = {0, 0};
    for (i = prePos.x, j = prePos.y + 1; j <= boundry.y; j++)
    {
        if (board[i][j] == 0)
        {
            pos.x = i;
            pos.y = j;
            return pos;
        }
    }
    for (i = prePos.x + 1; i <= boundry.x; i++)
    {
        for (j = 1; j <= boundry.y; j++)
        {
            if (board[i][j] == 0)
            {
                pos.x = i;
                pos.y = j;
                return pos;
            }
        }
    }
    return pos;
};

void findBoardType (struct position curPos)
{
    int i;
    struct position nextPos;
    if (curPos.x == curPos.y && curPos.x == 0)
    {
        outPut ();
    }
    else
    {

        for (i = 0; i < 2; i++)
        {
            if (placeADomino (curPos, i))
            {
                nextPos = getNextPosition (curPos);
                findBoardType (nextPos);
                delateADomino (curPos, i);
            }
        }

    }
}

int placeADomino (struct position pos, int type)
{
    if (type)
    {
        if (pos.y + 1 <= boundry.y && board[pos.x][pos.y + 1] == 0)
        {
            board[pos.x][pos.y] = no;
            board[pos.x][pos.y + 1] = no++;
            return 1;
        }
    }
    else
    {
        if (pos.x + 1 <= boundry.x && board[pos.x + 1][pos.y] == 0)
        {
            board[pos.x][pos.y] = no;
            board[pos.x + 1][pos.y] = no++;
            return 1;
        }
    }
    return 0;
}

void delateADomino (struct position pos, int type)
{
    if (type)
    {
        if (pos.y + 1 <= boundry.y)
        {
            board[pos.x][pos.y] = 0;
            board[pos.x][pos.y + 1] = 0;
        }
    }
    else
    {
        if (pos.x + 1 <= boundry.x)
        {
            board[pos.x][pos.y] = 0;
            board[pos.x + 1][pos.y] = 0;
        }
    }
    --no;
}

void outPut ()
{
    int i, j;
    printf ("NO : %d\n", typeNo++);
    for (i = 1; i <= boundry.x; i++)
    {
        for (j = 1; j <= boundry.y; j++)
        {
            printf ("%d", board[i][j]);
        }
        printf ("\n");
    }
    printf ("\n");
}

  递归确实简单很多,原来每次用循环写八皇后都得用好长时间,用了递归确实简单了。

  由于没有验证的方法,所以并不知道写的对不对。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值