递推算法——例题详解

递推算法的本质

    得到前后过程间的数字关系,将复杂问题分解为几个小问题

例一

数塔问题(倒推法)
图片百度的

问题
从顶至底找出一个路径,使其和最大
分析
从上往下推似乎有点麻烦
那么我们想想能不能从下而上(显然这样更好)
我们可以让n-1层的数加上他相邻的n的数中的最大值
如此反复
到最顶上必然是最大。
代码实现

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
	int n, i, j, a[101][101];
	cin >> n;
	for (i = 1; i <= n; i++)
		for (j = 1; j <= i; j++)
			cin >> a[i][j];
	for(i=n-1;i>=1;i--)
		for (j = 1; j <= i; j++)
		{
			if (a[i + 1][j] >= a[i + 1][j + 1])
				a[i][j] += a[i + 1][j];
			else
				a[i][j] += a[i + 1][j + 1];
		}
	cout << a[1][1] << endl;
	return 0;
}

例二
Fioonacci数列的非递归实现(1,1,2,3,5…)

分析
观察数列 我们得出递推式f(n)=f(n-1)+f(n-2)

#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
using namespace std;
int main()
{
	int f0 = 1, f1 = 1, f2=2;
	int n;
	cin >> n;
	for (int i = 3; i <= n; ++i)
	{
		f2 = f0 + f1;
		f0 = f2;
		f1 = f2;
	}
	cout << f2;
	return 0;
}

同样的类型还有 洛谷 数楼梯
还有经典的蓝桥杯题目 奶牛的递归

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
巡回置换问题是指在一个完全图中,每个顶点之间都有一条边,求一条经过每个顶点一次且仅一次的回路,使得回路的总长度最小。这个问题是一个NP难问题,因此通常使用启发式算来解决。其中一种启发式算是遗传算。 遗传算是一种模拟自然进化过程的优化算。在巡回置换问题中,可以将每个可能的回路看作一个个体,通过交叉、变异等操作来产生新的个体,并通过适应度函数来评估每个个体的优劣程度。经过多代进化,最终得到一个较优的回路。 以下是巡回置换问题的遗传算的Python实现: ```python import random # 生成初始种群 def generate_population(city_num, pop_size): population = [] for i in range(pop_size): chromosome = list(range(city_num)) random.shuffle(chromosome) population.append(chromosome) return population # 计算路径长度 def get_distance(city1, city2): return ((city1[0] - city2[0]) ** 2 + (city1[1] - city2[1]) ** 2) ** 0.5 def get_path_length(path, cities): length = 0 for i in range(len(path) - 1): length += get_distance(cities[path[i]], cities[path[i+1]]) length += get_distance(cities[path[-1]], cities[path[0]]) return length # 选择操作 def selection(population, cities): fitness_list = [1 / get_path_length(chromosome, cities) for chromosome in population] total_fitness = sum(fitness_list) probability_list = [fitness / total_fitness for fitness in fitness_list] selected_population = [] for i in range(len(population)): selected_population.append(random.choices(population, probability_list)[0]) return selected_population # 交叉操作 def crossover(parent1, parent2): child = [-1] * len(parent1) start = random.randint(0, len(parent1) - 1) end = random.randint(0, len(parent1) - 1) if start > end: start, end = end, start for i in range(start, end+1): child[i] = parent1[i] j = 0 for i in range(len(parent2)): if child[j] == -1: if parent2[i] not in child: child[j] = parent2[i] j += 1 else: j += 1 return child # 变异操作 def mutation(chromosome): index1 = random.randint(0, len(chromosome) - 1) index2 = random.randint(0, len(chromosome) - 1) chromosome[index1], chromosome[index2] = chromosome[index2], chromosome[index1] return chromosome # 遗传算主函数 def genetic_algorithm(city_list, pop_size, generation_num, crossover_rate, mutation_rate): population = generate_population(len(city_list), pop_size) for i in range(generation_num): population = selection(population, city_list) new_population = [] for j in range(pop_size): parent1 = random.choice(population) if random.random() < crossover_rate: parent2 = random.choice(population) child = crossover(parent1, parent2) else: child = parent1 if random.random() < mutation_rate: child = mutation(child) new_population.append(child) population = new_population best_path = min(population, key=lambda x: get_path_length(x, city_list)) best_length = get_path_length(best_path, city_list) return best_path, best_length # 测试 if __name__ == '__main__': city_list = [(0, 0), (1, 1), (2, 2), (3, 3), (4, 4)] best_path, best_length = genetic_algorithm(city_list, 100, 1000, 0.8, 0.1) print('最优路径:', best_path) print('最短路径长度:', best_length) ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值