感觉王岐上课讲的还行。
求最大公因数:
#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");
}
递归确实简单很多,原来每次用循环写八皇后都得用好长时间,用了递归确实简单了。
由于没有验证的方法,所以并不知道写的对不对。