关于Problem8的改进

最大连续乘积
本文介绍了一种高效算法,用于寻找1000位数字中连续五个数字的最大乘积。通过四种不同的方法逐步优化,显著提高了计算效率。
问题描述

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450


得到连续的五个数的最大乘积


思考:
每当我们得到当前五个数的乘积时,当挪动到下一个数,我们可以将保存的乘积乘以新的数除以之前五个数的第一个数。
如下
下标:0 1 2 3 4 5 6
数值:1 2 3 4 5 6 7
前五个数的乘积 result = 1*2*3*4*5
乘积中的第一个数first = 1
则求新的五个数的乘积 newResutl = result*6/first;
当然,实际运算时,我们还可以判断当前result和first是否为0

通过这样的运算,效率提高了很多~

自己逐步优化,写了四个方法
如下:
	public static int find_five_consecutive1() {
int result = 1;
int max = 0;
String str = read();
int begin = 0;
int end = str.length();
int step = 5;
for (int i = begin; i < end - step; i++) {
result = 1;
for (int j = 0; j < step; j++) {
result = result * Integer.parseInt(str.charAt(i + j) + "");
count_for1++;
}
if (result > max) {
max = result;
}
}
return max;
}

public static int find_five_consecutive2() {
int result = 1;
int max = 0;
String str = read();
int begin = 0;
int end = str.length();
int step = 5;
for (int i = begin; i < end - step; i++) {
result = 1;
for (int j = 0; j < step; j++) {
if (str.charAt(i + j) == '0') {
i = i + step + step - j;
break;
} else {
int number = Integer.parseInt(str.charAt(i + j) + "");
result = result * number;
count_for2++;
}
}
if (result > max) {
max = result;
}
}
return max;
}

public static int find_five_consecutive3() {
int result = 0;
String str = read();
int step = 5;
for (int i = 0; i < str.length(); i++) {
if (str.charAt(i) == '0') {
i = i + 5;
count_for3++;
} else {
int multi = 1;
for (int j = 0; j < step; j++) {
count_for3++;
char c = str.charAt(i + j);
if (c == '0') {
i = i + j + step;
break;
} else {
multi *= Integer.valueOf(c + "");
if (multi > result) {
result = multi;
}
}
}
}
}

return result;
}

public static int find_five_consecutive4() {
int result = 1;
int max = 1;
String str = read();
int step = 5;
int first = Integer.valueOf(str.charAt(0) + "");
for (int i = 0; i < step; i++) {
count_for4++;
char c = str.charAt(i);
result *= Integer.valueOf(c + "");
}
max = result;
for (int i = 5; i < str.length(); i++) {
if (result == 0) { // 重新计算
result = 1;
first = Integer.valueOf(str.charAt(i) + "");
for (int j = 0; j < step; j++) {
count_for4++;
result *= Integer.valueOf(str.charAt(i + j) + "");
}
if(result>max){
max = result;
}
i = i+step-1;
} else{
count_for4++;
if(str.charAt(i)=='0'){
result = 0;
i = i+4;
continue;
}else{
result = (result*Integer.valueOf(str.charAt(i)+""))/first;
first = Integer.valueOf(str.charAt(i-4)+"");
if(result>max){
max = result;
}
}
}
}

return max;
}


运算结果如下:
40824 count_for1:4975
40824 count_for2:2495
40824 count_for2:2305
40824 count_for2:820

效率提高的不是一般的多啊~
### 关于遗传算法改进的顶级会议论文 遗传算法(Genetic Algorithm, GA)是一种基于自然选择和遗传机制的优化算法,广泛应用于各种复杂问题的求解。然而,在实际应用中,遗传算法可能面临早熟收敛、后期搜索速度慢以及局部搜索能力较差等问题[^1]。为了解决这些问题,学者们提出了许多改进方法,包括引入新的选择策略、交叉算子和变异算子,以及将遗传算法与其他优化算法结合形成混合遗传算法等。 以下是一些关于遗传算法改进的顶级会议论文推荐: #### 1. **改进选择策略** 选择策略是遗传算法的核心之一,直接影响种群多样性和算法性能。文献《A Novel Selection Strategy for Genetic Algorithms in Constrained Optimization Problems》在IEEE Congress on Evolutionary Computation (CEC) 上发表,提出了一种新的选择策略,能够有效提高遗传算法在约束优化问题中的性能[^5]。 #### 2. **改进交叉算子** 交叉算子的设计对遗传算法的全局搜索能力至关重要。文献《Adaptive Crossover Operators for Genetic Algorithms》在ACM Genetic and Evolutionary Computation Conference (GECCO) 上发表,介绍了一种自适应交叉算子,可以根据问题特性动态调整交叉概率,从而提升算法的鲁棒性[^6]。 #### 3. **改进变异算子** 变异算子用于维持种群多样性,防止算法过早陷入局部最优。文献《Enhanced Mutation Operators for Genetic Algorithms in Continuous Optimization》在International Joint Conference on Neural Networks (IJCNN) 上发表,提出了一种增强型变异算子,适用于连续优化问题,显著提高了算法的探索能力[^7]。 #### 4. **混合遗传算法** 混合遗传算法通过结合其他优化技术(如粒子群优化、模拟退火等),进一步提升了遗传算法的性能。文献《Hybrid Genetic Algorithms with Particle Swarm Optimization for Global Optimization》在IEEE Symposium Series on Computational Intelligence (SSCI) 上发表,展示了如何将粒子群优化与遗传算法结合,以解决高维复杂优化问题[^8]。 #### 5. **多目标遗传算法** 多目标优化是遗传算法的重要应用场景之一。文献《Multi-Objective Genetic Algorithms with Adaptive Weighting Strategies》在IEEE World Congress on Computational Intelligence (WCCI) 上发表,提出了一种自适应权重策略的多目标遗传算法,能够在多个目标之间实现更好的权衡[^9]。 ### 示例代码:自适应交叉算子 以下是一个简单的自适应交叉算子实现示例: ```python import random def adaptive_crossover(parent1, parent2, problem_complexity): # 根据问题复杂度动态调整交叉概率 crossover_rate = 0.8 + 0.2 * problem_complexity if random.random() < crossover_rate: # 单点交叉 point = random.randint(1, len(parent1) - 1) child1 = parent1[:point] + parent2[point:] child2 = parent2[:point] + parent1[point:] return child1, child2 else: return parent1, parent2 ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值