动态规划算法
#include<iostream>
using namespace std;
int WalkCount1(int n)
{
if (n < 0) return 0;
if (n == 1) return 1;
else if (n == 2) return 2;
else
{
return WalkCount1(n - 1) + WalkCount1(n - 2);
}
}
int WalkCount2(int n)
{
int ret = 0;
if (n <= 0) return 0;
if (n == 1) return 1;
if (n == 2) return 2;
int* value = new int[n + 1];
value[0] = 0;
value[1] = 1;
value[2] = 2;
for (int i = 3; i <= n; i++)
{
value[i] = value[i - 1] + value[i - 2];
}
ret = value[n];
delete value;
return ret;
}
int main()
{
int n;
cout << "请输入台阶数:" << endl;
cin >> n;
cout << n << "级台阶共有" << WalkCount2(n) << "总走法。" << endl;
cout << n << "级台阶共有" << WalkCount2(n) << "总走法。" << endl;
system("pause");
return 0;
}
分支定界算法
参考A*算法
分治算法
#include<iostream>
using namespace std;
int BinarySearch(int *list, const int left, const int right, const int x)
{
if (left <= right)
{
int mid = (left + right) / 2;
if (x < list[mid])
{
return BinarySearch(list, left, mid - 1, x);
}
else if (x > list[mid])
{
return BinarySearch(list, mid + 1, right, x);
}
else
{
return mid;
}
}
return -1;
}
int main()
{
int girls[] = { 5,7,11,15,19,21,25,26,61,99 };
int index = BinarySearch(girls, 0, 9, 15);
cout << index << endl;
system("pause");
return 0;
}
回溯算法
#include<iostream>
using namespace std;
bool hasPathCore(const char* matrix, int rows, int cols, int row, int col, const char* str, int& pathLength, bool* visited);
bool hasPath(const char* matrix, int rows, int cols, const char* str)
{
if (matrix == nullptr || rows < 1 || cols < 1 || str == nullptr)
return false;
bool *visited = new bool[rows*cols];
memset(visited, 0, rows*cols);
int pathLength = 0;
for (int row = 0; row < rows; ++row)
{
for (int col = 0; col < cols; ++col)
{
if (hasPathCore(matrix, rows, cols, row, col, str, pathLength, visited))
{
return true;
}
}
}
delete[] visited;
return false;
}
bool hasPathCore(const char* matrix, int rows, int cols, int row, int col, const char* str, int& pathLength, bool* visited)
{
if (str[pathLength] == '\0')
{
return true;
}
bool hasPath = false;
if (row >= 0 && row < rows&&col >= 0 && col < cols&&matrix[row*cols + col] == str[pathLength] && !visited[row*cols + col])
{
++pathLength;
visited[row*cols + col] = true;
hasPath = hasPathCore(matrix, rows, cols, row, col - 1, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row - 1, col, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row, col + 1, str, pathLength, visited)
|| hasPathCore(matrix, rows, cols, row + 1, col, str, pathLength, visited);
if (!hasPath)
{
--pathLength;
visited[row*cols + col] = false;
}
return hasPath;
}
}
void Test(const char* testName, const char*matrix, int rows, int cols, const char* str, bool expected)
{
if (testName != nullptr)
cout << testName << " begins: ";
if (hasPath(matrix, rows, cols, str) == expected)
cout << "Passed." << endl;
else
cout << "FAILED." << endl;
}
void Test1()
{
const char* matrix = "ABTGCFCSJDEH";
const char* str = "BFCE";
Test("功能测试 1 ", (const char*)matrix, 3, 4, str, true);
}
void Test2()
{
const char* matrix = "ABCESFCSADEE";
const char* str = "SEE";
Test("功能测试 2 ", (const char*)matrix, 3, 4, str, true);
}
void Test3()
{
const char* matrix = "ABTGCFCSJDEH";
const char* str = "ABFB";
Test("功能测试 3 ", (const char*)matrix, 3, 4, str, true);
}
void Test4()
{
const char* matrix = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS";
const char* str = "SLHECCEIDEJFGGFIE";
Test("功能测试 4 ", (const char*)matrix, 5, 8, str, true);
}
void Test5()
{
const char* matrix = "ABCEHJIGSFCSLOPQADEEMNOEADIDEJFMVCEIFGGS";
const char* str = "SGGFIECVAASABCEHJIGQEM";
Test("功能测试 5 ", (const char*)matrix, 5, 8, str, true);
}
void Test6()
{
const char* matrix = "AAAAAAAAAAAA";
const char* str = "AAAAAAAAAAAA";
Test("边界值测试 6 ", (const char*)matrix, 3, 4, str, true);
}
void Test7()
{
const char* matrix = "A";
const char* str = "A";
Test("边界值测试 7 ", (const char*)matrix, 1, 1, str, true);
}
void Test8()
{
const char* matrix = "A";
const char* str = "B";
Test("边界值测试 8 ", (const char*)matrix, 1, 1, str, false);
}
void Test9()
{
Test("特殊情况测试 9 ", nullptr, 0, 0, nullptr, false);
}
int main()
{
Test1();
Test2();
Test3();
Test4();
Test5();
Test6();
Test7();
Test8();
Test9();
system("pause");
return 0;
}
贪心算法
#include<iostream>
#define N 7
int value[N] = { 1,2,5,10,20,50,100 };
int count[N] = { 9,9,9,9,9,9,9 };
int solve(int money)
{
int num = 0;
int i = 0;
for (i = N - 1; i >= 0; i--)
{
int j = money / value[i];
int c = j > count[i] ? count[i] : j;
if (c != 0)
{
printf("需要用面值 %d 的纸币 %d 张\n", value[i], c);
}
money -= c * value[i];
num += c;
if (money == 0)break;
}
if (money > 0)num = -1;
return num;
}
int main()
{
int money = 0;
int num = 0;
printf("请输入要找零的数目:\n");
scanf_s("%d", &money);
num = solve(money);
if (num == -1)
{
printf("找不开!\n");
}
else
{
printf("成功的使用至少 %d 张纸币实现找零!\n", num);
}
system("pause");
return 0;
}