五大核心算法

动态规划算法

#include<iostream>

using namespace std;

/*递归实现机器人台阶走法统计
参数:
	n	- 台阶个数
返回:上台阶总的走法

f(n) = f(n-1) + f(n-2);
*/

//分治算法
int WalkCount1(int n)
{
	if (n < 0) return 0;
	
	if (n == 1) return 1;		//一级台阶,一种走法
	else if (n == 2) return 2;	//二级台阶,两种走法
	else						//n 级台阶,n-1个台阶走法 + n-2个台阶走法
	{
		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;
	

	//数组用于存储走n个台阶的走法数
	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);

/*
	功能:查找矩阵中是否含有str指定的字串
	参数说明:
		matrix	输入矩阵
		rows	矩阵行数
		cols	矩阵列数
		str		要搜索的字符串
	返回值:是否找到 true 是,false 否
*/

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;
}

//ABTG
//CFCS
//JDEH
void Test1()
{
	const char* matrix = "ABTGCFCSJDEH";
	const char* str = "BFCE";

	Test("功能测试 1 ", (const char*)matrix, 3, 4, str, true);
}

//ABCE
//SFCS
//ADEE
void Test2()
{
	const char* matrix = "ABCESFCSADEE";
	const char* str = "SEE";

	Test("功能测试 2 ", (const char*)matrix, 3, 4, str, true);
}

//ABTG
//CFCS
//JDEH
void Test3()
{
	const char* matrix = "ABTGCFCSJDEH";
	const char* str = "ABFB";

	Test("功能测试 3 ", (const char*)matrix, 3, 4, str, true);
}

//ABCEHJIG
//SFCSLOPQ
//ADEEMNOE
//ADIDEJFM
//VCEIFGGS
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 };

/*贪心算法
	把子问题对应的局部最优解合成原来整个问题的应该近似最优解
*/


/*
	对输入的零钱数,找到至少要用的纸币数量
	参数:
		money - 要找/支付的零钱数
	返回:
		至少要用的纸币数量,-1表示找不开
*/

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;
}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值