mirror of
https://github.com/huihut/interview.git
synced 2026-03-13 10:02:55 +08:00
添加查找算法和SingleProblem
This commit is contained in:
160
Problems/ChessboardCoverageProblem/ChessboardCoverage.cpp
Normal file
160
Problems/ChessboardCoverageProblem/ChessboardCoverage.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
#include <cctype>
|
||||
|
||||
using namespace std;
|
||||
|
||||
int num_Now = 0; // 记录L型骨牌编号
|
||||
int **board = NULL; // 棋盘指针
|
||||
|
||||
// 函数声明
|
||||
void ChessBoard(int num_BoardTopLeftRow, int num_BoardTopLeftColumn, int num_SpecialRow, int num_SpecialColumn, int boardSize);
|
||||
|
||||
int main() {
|
||||
|
||||
int num_BoardTopLeftRow = 0, // 棋盘左上角的行号
|
||||
num_BoardTopLeftColumn = 0, // 棋盘左上角的列号
|
||||
num_SpecialRow = 0, // 特殊方格所在的行号
|
||||
num_SpecialColumn = 0, // 特殊方格所在的列号
|
||||
boardSize = 0, // 棋盘大小
|
||||
k = 0; // 构成的(2^k)*(2^k)个方格的棋盘
|
||||
|
||||
// 用户界面
|
||||
cout << "---------------- 棋盘覆盖问题 ----------------" << endl;
|
||||
cout << "请输入k(k>=0),构成(2^k)*(2^k)个方格的棋盘" << endl;
|
||||
|
||||
// 输入k值
|
||||
cin >> k;
|
||||
|
||||
// 判断输入数据合法性,包括检查输入是否为数字,k值是否大于0
|
||||
if (cin.fail() || k < 0)
|
||||
{
|
||||
cout << "输入k错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 计算棋盘大小
|
||||
boardSize = pow(2, k);
|
||||
|
||||
cout << "请输入特殊方格所在的行号和列号(从0开始,用空格隔开)" << endl;
|
||||
|
||||
// 输入特殊方格所在的行号和列号
|
||||
cin >> num_SpecialRow >> num_SpecialColumn;
|
||||
|
||||
// 判断输入数据合法性,包括检查输入是否为数字,特殊方格行号列号是否大于0,特殊方格行号列号是否不大于棋盘大小
|
||||
if (cin.fail() || num_SpecialRow < 0 || num_SpecialColumn < 0 || num_SpecialRow >= boardSize || num_SpecialColumn >= boardSize)
|
||||
{
|
||||
cout << "输入行号或列号错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 分配棋盘空间
|
||||
board = new int *[boardSize];
|
||||
for (auto i = 0; i < boardSize; i++)
|
||||
{
|
||||
board[i] = new int[boardSize];
|
||||
}
|
||||
|
||||
// 为特殊方格赋初值0
|
||||
board[num_SpecialRow][num_SpecialColumn] = 0;
|
||||
|
||||
//执行棋盘覆盖函数
|
||||
ChessBoard(num_BoardTopLeftRow, num_BoardTopLeftColumn, num_SpecialRow, num_SpecialColumn, boardSize);
|
||||
|
||||
// 显示输出
|
||||
cout << "------------------------------------------------" << endl;
|
||||
for (auto i = 0; i < boardSize; i++)
|
||||
{
|
||||
for (auto j = 0; j < boardSize; j++)
|
||||
{
|
||||
cout << board[i][j] << "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << "------------------------------------------------" << endl;
|
||||
|
||||
// 暂停查看结果
|
||||
system("pause");
|
||||
|
||||
// 释放内存
|
||||
for (int i = 0; i <= boardSize; i++)
|
||||
delete[] board[i];
|
||||
delete[] board;
|
||||
|
||||
// 指针置空
|
||||
board = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 棋盘覆盖函数
|
||||
void ChessBoard(int num_BoardTopLeftRow, int num_BoardTopLeftColumn, int num_SpecialRow, int num_SpecialColumn, int boardSize)
|
||||
{
|
||||
// 棋盘大小为1则直接返回
|
||||
if (boardSize == 1) return;
|
||||
|
||||
int num = ++num_Now, // L型骨牌编号
|
||||
size = boardSize / 2; // 分割棋盘,行列各一分为二
|
||||
|
||||
// 覆盖左上角子棋盘
|
||||
if (num_SpecialRow < num_BoardTopLeftRow + size && num_SpecialColumn < num_BoardTopLeftColumn + size)
|
||||
{
|
||||
// 递归覆盖含有特殊方格的子棋盘
|
||||
ChessBoard(num_BoardTopLeftRow, num_BoardTopLeftColumn, num_SpecialRow, num_SpecialColumn, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 用编号为num的L型骨牌覆盖右下角
|
||||
board[num_BoardTopLeftRow + size - 1][num_BoardTopLeftColumn + size - 1] = num;
|
||||
|
||||
// 递归覆盖其余棋盘
|
||||
ChessBoard(num_BoardTopLeftRow, num_BoardTopLeftColumn, num_BoardTopLeftRow + size - 1, num_BoardTopLeftColumn + size - 1, size);
|
||||
}
|
||||
|
||||
// 覆盖右上角子棋盘
|
||||
if (num_SpecialRow < num_BoardTopLeftRow + size && num_SpecialColumn >= num_BoardTopLeftColumn + size)
|
||||
{
|
||||
// 递归覆盖含有特殊方格的子棋盘
|
||||
ChessBoard(num_BoardTopLeftRow, num_BoardTopLeftColumn + size, num_SpecialRow, num_SpecialColumn, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 用编号为num的L型骨牌覆盖左下角
|
||||
board[num_BoardTopLeftRow + size - 1][num_BoardTopLeftColumn + size] = num;
|
||||
|
||||
// 递归覆盖其余棋盘
|
||||
ChessBoard(num_BoardTopLeftRow, num_BoardTopLeftColumn + size, num_BoardTopLeftRow + size - 1, num_BoardTopLeftColumn + size, size);
|
||||
}
|
||||
|
||||
// 覆盖左下角子棋盘
|
||||
if (num_SpecialRow >= num_BoardTopLeftRow + size && num_SpecialColumn < num_BoardTopLeftColumn + size)
|
||||
{
|
||||
// 递归覆盖含有特殊方格的子棋盘
|
||||
ChessBoard(num_BoardTopLeftRow + size, num_BoardTopLeftColumn, num_SpecialRow, num_SpecialColumn, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 用编号为num的L型骨牌覆盖右上角
|
||||
board[num_BoardTopLeftRow + size][num_BoardTopLeftColumn + size - 1] = num;
|
||||
|
||||
// 递归覆盖其余棋盘
|
||||
ChessBoard(num_BoardTopLeftRow + size, num_BoardTopLeftColumn, num_BoardTopLeftRow + size, num_BoardTopLeftColumn + size - 1, size);
|
||||
}
|
||||
|
||||
// 覆盖右下角子棋盘
|
||||
if (num_SpecialRow >= num_BoardTopLeftRow + size && num_SpecialColumn >= num_BoardTopLeftColumn + size)
|
||||
{
|
||||
// 递归覆盖含有特殊方格的子棋盘
|
||||
ChessBoard(num_BoardTopLeftRow + size, num_BoardTopLeftColumn + size, num_SpecialRow, num_SpecialColumn, size);
|
||||
}
|
||||
else
|
||||
{
|
||||
// 用编号为num的L型骨牌覆盖左上角
|
||||
board[num_BoardTopLeftRow + size][num_BoardTopLeftColumn + size] = num;
|
||||
|
||||
// 递归覆盖其余棋盘
|
||||
ChessBoard(num_BoardTopLeftRow + size, num_BoardTopLeftColumn + size, num_BoardTopLeftRow + size, num_BoardTopLeftColumn + size, size);
|
||||
}
|
||||
}
|
||||
BIN
Problems/ChessboardCoverageProblem/ChessboardCoverage.exe
Normal file
BIN
Problems/ChessboardCoverageProblem/ChessboardCoverage.exe
Normal file
Binary file not shown.
24
Problems/ChessboardCoverageProblem/README.md
Normal file
24
Problems/ChessboardCoverageProblem/README.md
Normal file
@@ -0,0 +1,24 @@
|
||||
|
||||
## 棋盘覆盖问题
|
||||
|
||||
### 代码
|
||||
|
||||
[棋盘覆盖问题代码](ChessboardCoverage.cpp)
|
||||
|
||||
### 问题说明
|
||||
|
||||
在一个2^k * 2^k个方格组成的棋盘中,恰有一个方格与其它方格不同,称该方格为一特殊方格。
|
||||
|
||||
棋盘覆盖问题就是要用图示的4种不同形态的L型骨牌覆盖给定棋盘上除特殊方格之外的所有方格,且任何2个L型骨牌不得重叠覆盖。
|
||||
|
||||

|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序用分治法的思想解决了棋盘覆盖问题,显示输出
|
||||
|
||||
### 代码简述
|
||||
|
||||
用户输入数据,程序输入检测,动态分配空间,调用棋盘覆盖函数,把计算结果存储到board(二维数组指针),显示输出。
|
||||
|
||||
其中棋盘覆盖函数用分治的思想把棋盘分成四份,递归求解。
|
||||
34
Problems/KnapsackProblem/README.md
Normal file
34
Problems/KnapsackProblem/README.md
Normal file
@@ -0,0 +1,34 @@
|
||||
|
||||
## 背包问题
|
||||
|
||||
### 代码
|
||||
|
||||
[背包问题代码](pack.cpp)
|
||||
|
||||
### 问题说明
|
||||
|
||||
有N件物品和一个容量为V的背包。
|
||||
|
||||
第i件物品的重量是w[i],价值是v[i]。
|
||||
|
||||
求解将哪些物品装入背包可使这些物品的重量总和不超过背包容量,
|
||||
|
||||
且价值总和最大。
|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序用动态规划的思想解决了背包问题,并用了两种算法:
|
||||
迭代法、递归法。在迭代法中实现了打印背包问题的表格。
|
||||
|
||||
### 代码简述
|
||||
|
||||
通过用户输入数据,程序输入检测,动态分配空间,选择算法,
|
||||
用动态规划的思想求解背包问题。
|
||||
|
||||
#### 迭代法:
|
||||
通过遍历n行W列,迭代每行每列的值,并把最优解放到
|
||||
n行(在数组中为第n+1行)W列(在数组中为第W+1列)中。
|
||||
|
||||
#### 递归法:
|
||||
通过每次返回前i个物品和承重为j的最优解,
|
||||
递归计算总背包问题的最优解。
|
||||
165
Problems/KnapsackProblem/pack.cpp
Normal file
165
Problems/KnapsackProblem/pack.cpp
Normal file
@@ -0,0 +1,165 @@
|
||||
#include <iostream>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
|
||||
int *w = NULL; // 存储每件物品重量的数组指针
|
||||
int *v = NULL; // 存储每件物品价值的数组指针
|
||||
int **T = NULL; // 存储背包问题表格的数组指针
|
||||
int n; // 物品个数n
|
||||
int W; // 背包总承重W
|
||||
|
||||
// 返回两个值的最大值
|
||||
int max(int a, int b)
|
||||
{
|
||||
return (a > b) ? a : b;
|
||||
}
|
||||
|
||||
// 迭代法,能显示背包问题的表格
|
||||
void packIterative()
|
||||
{
|
||||
// 循环遍历n行
|
||||
for (int i = 1; i <= n; ++i)
|
||||
{
|
||||
// 循环遍历W列
|
||||
for (int j = 1; j <= W; ++j)
|
||||
{
|
||||
//第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值
|
||||
if (w[i] <= j)
|
||||
T[i][j] = max(v[i] + T[i - 1][j - w[i]], T[i - 1][j]);
|
||||
|
||||
// 第i个物品不能装下,则递归装i-1个
|
||||
else
|
||||
T[i][j] = T[i - 1][j];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 递归法,不支持显示背包问题的表格
|
||||
int packRecursive(int i, int j, int *w, int *v)
|
||||
{
|
||||
// 结束条件(初始条件),i或者j为0时最大总价值为0
|
||||
if (i == 0 || j == 0)
|
||||
return 0;
|
||||
|
||||
// 第i个物品不能装下,则递归装i-1个
|
||||
if (w[i] > j)
|
||||
return packRecursive(i - 1, j, w, v);
|
||||
|
||||
//第i个物品能装下,则比较包括第i个物品和不包括第i个物品,取其最大值
|
||||
else
|
||||
return max(v[i] + packRecursive(i - 1, j - w[i], w, v), packRecursive(i - 1, j, w, v));
|
||||
}
|
||||
|
||||
// 打印背包问题的表格
|
||||
void printT(int n, int W)
|
||||
{
|
||||
// 打印n行
|
||||
for (auto i = 0; i <= n; i++)
|
||||
{
|
||||
// 打印行数
|
||||
cout << i << ":\t";
|
||||
|
||||
// 打印W列
|
||||
for (int w = 0; w <= W; w++)
|
||||
{
|
||||
cout << T[i][w] << "\t";
|
||||
}
|
||||
|
||||
// 换行
|
||||
cout << endl;
|
||||
}
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
|
||||
cout << "---------------- 背包问题 ----------------" << endl;
|
||||
cout << "请输入物品数 n (n>=0) " << endl;
|
||||
|
||||
// 输入背包数
|
||||
cin >> n;
|
||||
|
||||
if (cin.fail() || n < 0)
|
||||
{
|
||||
cout << "输入n错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
cout << "请输入背包承重量 W (W>=0) " << endl;
|
||||
|
||||
// 输入背包承重量
|
||||
cin >> W;
|
||||
|
||||
if (cin.fail() || W < 0)
|
||||
{
|
||||
cout << "输入W错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 分配空间
|
||||
// 对w和v分配n+1大小
|
||||
w = new int[n+1];
|
||||
v = new int[n+1];
|
||||
|
||||
// 对T分配n+1行,并初始化为0
|
||||
T = new int *[n + 1]();
|
||||
// 对T分配W+1列,并初始化为0
|
||||
for (auto i = 0; i <= n; i++)
|
||||
{
|
||||
T[i] = new int[W + 1]();
|
||||
}
|
||||
|
||||
// 输入背包的重量和价值
|
||||
for (auto i = 1; i <= n; i++)
|
||||
{
|
||||
cout << "请输入第 " << i << " 个背包的重量和价值(用空格隔开)" << endl;
|
||||
cin >> w[i] >> v[i];
|
||||
if (cin.fail() || w[i] < 0 || v[i] < 0)
|
||||
{
|
||||
cout << "输入错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "------------------------------------------------" << endl;
|
||||
cout << "请选择算法:" << endl;
|
||||
cout << "【1】迭代法" << endl;
|
||||
cout << "【2】递归法" << endl;
|
||||
cout << "------------------------------------------------" << endl;
|
||||
|
||||
int choose;
|
||||
|
||||
// 输入算法的选择
|
||||
cin >> choose;
|
||||
switch (choose)
|
||||
{
|
||||
case 1:
|
||||
{
|
||||
// 迭代法,能显示背包问题的表格
|
||||
packIterative();
|
||||
cout << "能装下物品的最大价值为 " << T[n][W] << endl;
|
||||
cout << "------------------------------------------------" << endl;
|
||||
printT(n, W);
|
||||
break;
|
||||
}
|
||||
case 2:
|
||||
{
|
||||
// 递归法,不支持显示背包问题的表格
|
||||
cout << "能装下物品的最大价值为 " << packRecursive(n, W, w, v) << endl;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
{
|
||||
cout << "输入错误!" << endl;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
cout << "------------------------------------------------" << endl;
|
||||
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
BIN
Problems/KnapsackProblem/pack.exe
Normal file
BIN
Problems/KnapsackProblem/pack.exe
Normal file
Binary file not shown.
33
Problems/NeumannNeighborProblem/Formula/Neumann2_3_12.cpp
Normal file
33
Problems/NeumannNeighborProblem/Formula/Neumann2_3_12.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//通项法
|
||||
int Neumann2_3_12(int n) {
|
||||
|
||||
//通项公式的求解请查看说明文档
|
||||
return 2 * n*n + 2 * n + 1;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n = 0, a = 0;
|
||||
|
||||
printf("------冯诺依曼邻居问题------\n");
|
||||
printf("已知:\n");
|
||||
printf(" 0 阶冯诺依曼邻居的元胞数为 1 \n");
|
||||
printf(" 1 阶冯诺依曼邻居的元胞数为 5 \n");
|
||||
printf(" 2 阶冯诺依曼邻居的元胞数为 13 \n");
|
||||
printf("求:\n");
|
||||
printf(" n 阶冯诺依曼邻居的元胞数\n");
|
||||
printf("----------------------------\n");
|
||||
printf("请输入n\n");
|
||||
scanf("%d", &n);
|
||||
|
||||
//用通项公式求解
|
||||
a = Neumann2_3_12(n);
|
||||
|
||||
printf("------------通项法-------------\n");
|
||||
printf(" %d 阶冯诺依曼邻居的元胞数为 %d\n", n, a);
|
||||
|
||||
getchar();
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
BIN
Problems/NeumannNeighborProblem/Formula/Neumann2_3_12.exe
Normal file
BIN
Problems/NeumannNeighborProblem/Formula/Neumann2_3_12.exe
Normal file
Binary file not shown.
38
Problems/NeumannNeighborProblem/Formula/README.md
Normal file
38
Problems/NeumannNeighborProblem/Formula/README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
## 冯诺依曼邻居问题(通项公式)
|
||||
|
||||
### 代码
|
||||
|
||||
[冯诺依曼邻居问题(通项公式)代码](Neumann2_3_12.cpp)
|
||||
|
||||
### 问题说明
|
||||
|
||||
某算法从一个1×1的方格开始,每次都会在上次图形的周围再加上一圈方格,在第n次的时候要生成多少个方格?下图给出了n = 0,1,2是的结果。
|
||||
|
||||

|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序使用通项公式求解。
|
||||
|
||||
### 代码简述
|
||||
|
||||
若设第n次生成的方格数是a(n),则:
|
||||
|
||||
a(1) = a(0) + 4 * 1
|
||||
a(2) = a(1) + 4 * 2
|
||||
a(3) = a(2) + 4 * 3
|
||||
...
|
||||
a(n) = a(n-1) + 4 * n
|
||||
|
||||
化简可得:
|
||||
|
||||
a(n) - a(1) = 4 * (n + (n-1) + ... + 2 )
|
||||
|
||||
即:
|
||||
|
||||
a(n) = 2 * n*n + 2 * n + 1
|
||||
|
||||
则可得出a(n)的通项公式,即可用通项公式直接求解。
|
||||
|
||||
在程序中用Neumann2_3_12函数返回a(n)的值。
|
||||
13
Problems/NeumannNeighborProblem/README.md
Normal file
13
Problems/NeumannNeighborProblem/README.md
Normal file
@@ -0,0 +1,13 @@
|
||||
|
||||
## 冯诺依曼邻居问题
|
||||
|
||||
### 问题说明
|
||||
|
||||
某算法从一个1×1的方格开始,每次都会在上次图形的周围再加上一圈方格,在第n次的时候要生成多少个方格?下图给出了n = 0,1,2是的结果。
|
||||
|
||||

|
||||
|
||||
### 解法
|
||||
|
||||
* [通项公式解法](Formula)
|
||||
* [递推关系解法](Recursive)
|
||||
36
Problems/NeumannNeighborProblem/Recursive/Neumann2_4_12.cpp
Normal file
36
Problems/NeumannNeighborProblem/Recursive/Neumann2_4_12.cpp
Normal file
@@ -0,0 +1,36 @@
|
||||
#include <stdio.h>
|
||||
|
||||
//递归法
|
||||
int Neumann2_4_12(int n) {
|
||||
|
||||
//由图可知第0次有1个方格
|
||||
if (n == 0) return 1;
|
||||
|
||||
//递推关系的求解请查看说明文档
|
||||
return Neumann2_4_12(n - 1) + 4 * n;
|
||||
}
|
||||
|
||||
int main() {
|
||||
int n = 0, a = 0;
|
||||
|
||||
printf("------冯诺依曼邻居问题------\n");
|
||||
printf("已知:\n");
|
||||
printf(" 0 阶冯诺依曼邻居的元胞数为 1 \n");
|
||||
printf(" 1 阶冯诺依曼邻居的元胞数为 5 \n");
|
||||
printf(" 2 阶冯诺依曼邻居的元胞数为 13 \n");
|
||||
printf("求:\n");
|
||||
printf(" n 阶冯诺依曼邻居的元胞数\n");
|
||||
printf("----------------------------\n");
|
||||
printf("请输入n\n");
|
||||
scanf("%d", &n);
|
||||
|
||||
//建立递推关系,使用递归求解
|
||||
a = Neumann2_4_12(n);
|
||||
|
||||
printf("------------通项法-------------\n");
|
||||
printf(" %d 阶冯诺依曼邻居的元胞数为 %d\3n", n, a);
|
||||
|
||||
getchar();
|
||||
getchar();
|
||||
return 0;
|
||||
}
|
||||
BIN
Problems/NeumannNeighborProblem/Recursive/Neumann2_4_12.exe
Normal file
BIN
Problems/NeumannNeighborProblem/Recursive/Neumann2_4_12.exe
Normal file
Binary file not shown.
38
Problems/NeumannNeighborProblem/Recursive/README.md
Normal file
38
Problems/NeumannNeighborProblem/Recursive/README.md
Normal file
@@ -0,0 +1,38 @@
|
||||
|
||||
## 冯诺依曼邻居问题(递推关系)
|
||||
|
||||
### 代码
|
||||
|
||||
[冯诺依曼邻居问题(递推关系)代码](Neumann2_4_12.cpp)
|
||||
|
||||
### 问题说明
|
||||
|
||||
某算法从一个1×1的方格开始,每次都会在上次图形的周围再加上一圈方格,在第n次的时候要生成多少个方格?下图给出了n = 0,1,2是的结果。
|
||||
|
||||

|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序使用递推关系求解。
|
||||
|
||||
### 代码简述
|
||||
|
||||
若设第n次生成的方格数是a(n),则:
|
||||
|
||||
a(1) = a(0) + 4 * 1
|
||||
a(2) = a(1) + 4 * 2
|
||||
a(3) = a(2) + 4 * 3
|
||||
...
|
||||
a(n) = a(n-1) + 4 * n
|
||||
|
||||
则可得:
|
||||
|
||||
a(n) = a(n - 1) + 4 * n
|
||||
|
||||
然后在代码中使用递归法,递归结束条件为n = 0,即
|
||||
|
||||
if (n == 0) return 1;
|
||||
|
||||
则可写出递归法的代码。
|
||||
|
||||
在程序中用Neumann2_4_12函数进行递归求解。
|
||||
102
Problems/RoundRobinProblem/MatchTable.cpp
Normal file
102
Problems/RoundRobinProblem/MatchTable.cpp
Normal file
@@ -0,0 +1,102 @@
|
||||
#include <iostream>
|
||||
#include <math.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
// 循环赛日程安排函数声明
|
||||
void MatchTable(int k, int n, int **table);
|
||||
|
||||
int main()
|
||||
{
|
||||
int n = 0, k = 0;
|
||||
|
||||
// 用户界面
|
||||
cout << "---------------- 循环赛日程安排问题 ----------------" << endl;
|
||||
cout << "请输入k(k>=0),构成 n=(2^k) 个选手的循环赛" << endl;
|
||||
|
||||
// 输入k值
|
||||
cin >> k;
|
||||
|
||||
// 判断输入数据合法性,包括检查输入是否为数字,k值是否大于0
|
||||
if (cin.fail() || k < 0)
|
||||
{
|
||||
cout << "输入k错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 计算比赛日程表大小
|
||||
n = pow(2, k);
|
||||
|
||||
// 分配日程表空间
|
||||
int **table = new int *[n + 1];
|
||||
for (int i = 0; i <= n; i++)
|
||||
{
|
||||
table[i] = new int[n + 1];
|
||||
}
|
||||
|
||||
// 进行循环赛日程安排,生成日程表
|
||||
MatchTable(k, n, table);
|
||||
|
||||
// 显示输出
|
||||
cout << "------------------------------------------------" << endl;
|
||||
for (int i = 1; i <= n; i++)
|
||||
{
|
||||
for (int j = 1; j <= n; j++)
|
||||
{
|
||||
cout << table[i][j] << "\t";
|
||||
}
|
||||
cout << endl;
|
||||
}
|
||||
cout << "------------------------------------------------" << endl;
|
||||
|
||||
// 暂停查看结果
|
||||
system("pause");
|
||||
|
||||
// 释放内存
|
||||
for (int i = 0; i <= n; i++)
|
||||
delete[] table[i];
|
||||
delete[] table;
|
||||
|
||||
// 指针置空
|
||||
table = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 进行循环赛日程安排,生成日程表
|
||||
void MatchTable(int k, int n, int **table)
|
||||
{
|
||||
// 设置日程表第一行的值
|
||||
for (int i = 1; i <= n; i++)
|
||||
table[1][i] = i;
|
||||
|
||||
// 每次填充的起始填充位置
|
||||
int begin = 1;
|
||||
|
||||
// 用分治法分separate份,循环求解
|
||||
for (int separate = 1; separate <= k; separate++)
|
||||
{
|
||||
// 日程表进行划分
|
||||
n /= 2;
|
||||
|
||||
// flag为每一小份的列的标记
|
||||
for (int flag = 1; flag <= n; flag++)
|
||||
{
|
||||
// 操作行
|
||||
for (int i = begin + 1; i <= 2 * begin; i++)
|
||||
{
|
||||
// 操作列
|
||||
for (int j = begin + 1; j <= 2 * begin; j++)
|
||||
{
|
||||
// 把左上角的值赋给右下角
|
||||
table[i][j + (flag - 1) * begin * 2] = table[i - begin][j + (flag - 1) * begin * 2 - begin];
|
||||
// 把右上角的值赋给左下角
|
||||
table[i][j + (flag - 1) * begin * 2 - begin] = table[i - begin][j + (flag - 1) * begin * 2];
|
||||
}
|
||||
}
|
||||
}
|
||||
// 进入日程表的下一个划分进行填充
|
||||
begin *= 2;
|
||||
}
|
||||
}
|
||||
BIN
Problems/RoundRobinProblem/MatchTable.exe
Normal file
BIN
Problems/RoundRobinProblem/MatchTable.exe
Normal file
Binary file not shown.
31
Problems/RoundRobinProblem/README.md
Normal file
31
Problems/RoundRobinProblem/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
## 循环赛日程安排问题
|
||||
|
||||
### 代码
|
||||
|
||||
[循环赛日程安排问题代码](MatchTable.cpp)
|
||||
|
||||
### 问题说明
|
||||
|
||||
设有n=2k个选手要进行网球循环赛,
|
||||
要求设计一个满足以下要求的比赛日程表:
|
||||
|
||||
(1)每个选手必须与其他n-1个选手各赛一次;
|
||||
(2)每个选手一天只能赛一次。
|
||||
|
||||
按此要求,可将比赛日程表设计成一个 n 行n-1列的二维表,
|
||||
其中,第 i 行第 j 列表示和第 i 个选手在第 j 天比赛的选手。
|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序运用分治的思想,实现了循环赛日程安排问题的求解,
|
||||
生成日程表,输出。
|
||||
|
||||
### 代码简述
|
||||
|
||||
通过用户输入数据,程序输入检测,动态分配空间,
|
||||
调用生成日程表函数,显示输出。
|
||||
|
||||
其中,生成日程表函数运用分治的思想,分成separate份,
|
||||
先安排第一行(第一份),然后每一份填充,最终求解完毕,
|
||||
生成日程表。
|
||||
31
Problems/TubingProblem/README.md
Normal file
31
Problems/TubingProblem/README.md
Normal file
@@ -0,0 +1,31 @@
|
||||
|
||||
## 输油管道问题
|
||||
|
||||
### 代码
|
||||
|
||||
[输油管道问题代码](Tubing.cpp)
|
||||
|
||||
### 问题说明
|
||||
|
||||
某石油公司计划建造一条由东向西的主输油管道。
|
||||
该管道要穿过一个有n 口油井的油田。
|
||||
从每口油井都要有一条输油管道沿最短路经(或南或北)与主管道相连。
|
||||
如果给定n口油井的位置,即它们的x 坐标(东西向)和y 坐标(南北向),
|
||||
应如何确定主管道的最优位置,
|
||||
即使各油井到主管道之间的输油管道长度总和最小的位置?
|
||||
|
||||
### 功能说明
|
||||
|
||||
本程序用排序求中值的方法求解输油管道问题。
|
||||
|
||||
### 代码简述
|
||||
|
||||
通过用户输入数据(只输入油井数n、每个油井的y坐标),
|
||||
程序输入检测,动态分配空间,排序(使用快速排序),
|
||||
求出中间值,输出。
|
||||
|
||||
输出有以下两种情况:
|
||||
|
||||
1. 当n为奇数,则最优位置为y数组的第n/2个油井的y坐标
|
||||
|
||||
2. 当n为偶数,则最优位置为y数组的中间两个油井的y坐标的区间
|
||||
116
Problems/TubingProblem/Tubing.cpp
Normal file
116
Problems/TubingProblem/Tubing.cpp
Normal file
@@ -0,0 +1,116 @@
|
||||
#include <iostream>
|
||||
using namespace std;
|
||||
|
||||
// 油井y坐标指针
|
||||
float * y = NULL;
|
||||
|
||||
// 快速排序
|
||||
void quick_sort(int low, int high)
|
||||
{
|
||||
if (low >= high) // 结束标志
|
||||
return;
|
||||
int first = low; // 低位下标
|
||||
int last = high; // 高位下标
|
||||
float key = y[first]; // 设第一个为基准
|
||||
|
||||
while (first < last)
|
||||
{
|
||||
// 将比第一个小的移到前面
|
||||
while (first < last && y[last] >= key)
|
||||
last--;
|
||||
if (first < last)
|
||||
y[first++] = y[last];
|
||||
|
||||
// 将比第一个大的移到后面
|
||||
while (first < last && y[first] <= key)
|
||||
first++;
|
||||
if (first < last)
|
||||
y[last--] = y[first];
|
||||
}
|
||||
// 基准置位
|
||||
y[first] = key;
|
||||
// 前半递归
|
||||
quick_sort(low, first - 1);
|
||||
// 后半递归
|
||||
quick_sort(first + 1, high);
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int n; // 油井数
|
||||
float mid; // y数组的中间位置的数
|
||||
float minDistance = 0; // 各油井到主管道之间的管道长度总和最小位置
|
||||
|
||||
cout << "---------------- 输油管问题 ----------------" << endl;
|
||||
cout << "请输入油井数 n (n>=0) " << endl;
|
||||
|
||||
// 输入油井数
|
||||
cin >> n;
|
||||
|
||||
// 判断输入数据合法性,包括检查输入是否为数字,k值是否大于0
|
||||
if (cin.fail() || n < 0)
|
||||
{
|
||||
cout << "输入n错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 分配n个y坐标存储空间
|
||||
y = new float[n];
|
||||
|
||||
cout << "请输入 " << n << " 个油井的 y 坐标(用空格隔开)" << endl;
|
||||
|
||||
// 输入油井的 y 坐标
|
||||
for (auto i = 0; i < n; i++)
|
||||
{
|
||||
cin >> y[i];
|
||||
}
|
||||
|
||||
// 判断输入数据合法性
|
||||
if (cin.fail())
|
||||
{
|
||||
cout << "输入y坐标错误!" << endl;
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
|
||||
// 运用快速排序对y坐标数组进行排序
|
||||
quick_sort(0, n - 1);
|
||||
|
||||
// 计算y数组的中间位置的数
|
||||
mid = y[n / 2];
|
||||
|
||||
// 计算各个油井到主输油管的长度之和
|
||||
for (auto i = 0; i < n; i++)
|
||||
{
|
||||
minDistance += abs(y[i] - mid);
|
||||
}
|
||||
|
||||
// 显示输出
|
||||
cout << "------------------------------------------------" << endl;
|
||||
// 判断油井奇偶,做不同的输出
|
||||
if (n & 1)
|
||||
{
|
||||
// n为奇数,则最优位置为y数组的第n/2个油井的y坐标
|
||||
cout << "主管道的最优位置为:y = " << mid << endl;
|
||||
}
|
||||
else
|
||||
{
|
||||
// n为偶数,则最优位置为y数组的中间两个油井的y坐标的区间
|
||||
cout << "主管道的最优位置为:y = [" << y[n / 2 - 1] << "," << mid << "]" << endl;
|
||||
}
|
||||
// 输出各油井到主管道之间的管道总长度
|
||||
cout << "各油井到主管道之间的管道总长度为:" << minDistance << endl;
|
||||
cout << "------------------------------------------------" << endl;
|
||||
|
||||
// 暂停查看结果
|
||||
system("pause");
|
||||
|
||||
// 释放内存
|
||||
delete[] y;
|
||||
|
||||
// 指针置空
|
||||
y = NULL;
|
||||
|
||||
return 0;
|
||||
}
|
||||
BIN
Problems/TubingProblem/Tubing.exe
Normal file
BIN
Problems/TubingProblem/Tubing.exe
Normal file
Binary file not shown.
Reference in New Issue
Block a user