- 浏览: 138811 次
- 性别:
- 来自: 01
文章分类
最新评论
-
kingj:
将if(node.RightTree==null||node. ...
在二元树中找出和为某一值的所有路径 -
kingj:
非递归的算法在下面这种情况下会有问题 ...
在二元树中找出和为某一值的所有路径 -
houxinbin:
DateUtil.getTimestampFromGregor ...
使用JFreeChart显示 Java 虚拟机中的空闲内存量 -
坏小子46110:
我在build comm.js的时候有个这个异常 不知道怎么解 ...
使用Java实现登陆WebQQ(带源码) -
to_zoe_yang:
公子_小王 写道怎么下载不下来呢? 估计TX现在肯定改接口了都 ...
使用Java实现登陆WebQQ(带源码)
问题描述
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
通过这样的运算,效率提高了很多~
自己逐步优化,写了四个方法
如下:
运算结果如下:
40824 count_for1:4975
40824 count_for2:2495
40824 count_for2:2305
40824 count_for2:820
效率提高的不是一般的多啊~
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
效率提高的不是一般的多啊~
发表评论
-
Problem 52
2011-09-07 19:37 844问题描述: It can be seen that th ... -
Problem 51
2011-09-06 19:46 927问题描述: By replacing the ... -
Problem 50
2011-09-03 14:53 885问题描述: The prime 41, can b ... -
Problem 49
2011-08-28 15:34 887问题描述: The arithmetic seq ... -
Problem 47
2011-08-27 16:10 499问题描述: The first two co ... -
Problem 46
2011-08-26 18:08 554问题描述: It was proposed by Chr ... -
Problem 45
2011-08-25 18:24 910问题描述: Triangle, pentagona ... -
Problem43
2011-08-24 16:32 849问题描述: The number, 1406357289 ... -
Problem 42
2011-08-23 09:18 809问题描述: The nth term of the se ... -
Problem 41
2011-08-19 15:08 674问题描述: We shall say that an n ... -
Problem 40
2011-08-18 14:27 749算法描述: An irrational decimal ... -
Problem 39
2011-08-17 15:28 828问题描述: If p is the perimeter ... -
Problem 38
2011-08-17 15:27 725问题描述: Take the number 192 an ... -
Problem 37
2011-08-17 15:26 713问题描述: The number 3797 has an ... -
Problem 36
2011-08-17 15:23 764问题描述: The decimal number, 58 ... -
Problem 35
2011-08-17 15:22 678问题描述: The number, 197, is ca ... -
Problem 34
2011-08-17 15:20 755问题描述: 145 is a curious numbe ... -
Problem 33
2011-08-17 15:18 727问题描述: The fraction 49/98 is ... -
Problem 32
2011-08-17 15:17 852问题描述: We shall say that an n ... -
Problem 30
2011-08-17 15:11 449问题描述: Surprisingly there ...
相关推荐
利用蛙跳算法求解流水车间调度问题,算法上做了相应改进
为了解决应急条件下的物资分配,在研究蚁群算法解决传统VRP(vehicle routing problem)的基础上,通过加入动态路况子对VRP数学模型进行改进,提出蚁群算法对改进后的车辆路路径问题的数学模型,并利用陕西省21个...
论文研究-多资源约束下改进NSGA-II算法的手术调度.pdf, 针对手术调度涉及因素众多难以优化的问题, 在考虑手术台、执刀医师等资源约束,构建了以病人满意度及手 术总流程...
使用混合遗传算法来解决单个集...在所提出算法的框架内,使用了一种特殊的个体二倍体表示方案,并采用了一种改进的启发式包装方法,该方法源自最深的左下填充(DBLF)包装方法,用于实现盒子的 3D 包装。 效果展示: ...
It's not a technology problem, it's a process problem. If you do not understand how to develop software then chances are very good that you won't be very successful doing so, and our industry’s 80-...
Foresee a p该项目是一个简单的案例,它代表径向基函数 RBF 神经网络意味着建立径向基函数来预测问题。 它可以改进,特别是使用和收益问题。神经网络的问题
为解决柔性流水车间调度问题(flexible flow shop scheduling problem,FFSP),提出了一种基于精英个体集的自适应蝙蝠算法(self-adaptive elite bat algorithm,SEBA)。针对蝙蝠算法存在求解离散问题具有局限性、...
针对旅行商问题(Travelling Salesman Problem,TSP)的遗传算法的大规模操作,需要大量运算时间而且容易造成局部最优解,提出一种并行混合遗传算法。该方法基于MPI并行环境,利用种群中选择、交叉、变异操作的并行...
8D:Discplines of Problem Solving;解决问题的8个步骤,用于根本原因分析、问题、矫正、消灭反复出现的品质问题的管理方法,用助于产品和流程改进,特别强调团队合作,适合以团队为单位来解决问题。
求解分裂可行问题的改进的松弛CQ方法,李敏,,本文提出求解分裂可行问题的改进的松弛CQ方法。这些新方法是基于修正的松弛CQ算法,沿着下降方向搜索最优步长产生新的迭代点。在�
首先构建了机组组合问题的基本模型,在此基础上,进一步考虑负荷侧管理,也就是需求响应,在调控过程中通过补偿引导负荷侧积极进行需求响应,在模型的求解上,采用了一种基于改进二进制粒子群算法的求解方法,相较于...
为了解决柔性流水车间组批排产优化问题(flexible flow shop scheduling problem with batch process machines,FFSP-BPM),对组批加工环节中工件加工方式的变化以及工件的...
创建您自己的算法,如果您有任何改进的想法,请随时与我联系! 该工具使用Java信号量和自制的用户界面。 该应用程序需要Java 8+才能正常工作。 我该如何测试? 您只需在下载可用的最新版本。 然后,通过在JAR文件...
该程序复现《A Modified Binary PSO to solve the Thermal Unit Commitment Problem》,主要做的是一个考虑需求响应的机组组合问题,首先构建了机组组合问题的基本模型,在此基础上,进一步考虑负荷侧管理,也就是...
d-无爪图中最大权独立集问题的一种改进逼近算法_An Improved Approximation Algorithm for the Maximum Weight Independent Set Problem in d-Claw Free Graphs.pdf
多车场多车型车辆路径问题的改进遗传算法,车辆路径问题(Vehicle routing problem,VRP)由Dantzing和Ramser于1959年首次提出,它是指对一系列发货点(或收货点),组织适当的行车路线,满足客户的需求,并在一定的约束条件下...
The simulation results indicate that the improved algorithm can effectively improve global optimization ability, and much more robust and practical value in solving the time-cost optimization problem ...
研究了带时间窗的车辆路径问题(Vehicle Routing Problem with Time Windows,VRPTW),建立了数学模型,并设计了求解VRPTW的离散差分进化混合算法。算法采用随机车辆配载方法构造初始解,并通过构造新的变异和交叉...
clustering problem. A new way of integrating IGA and PSO is explored in the paper. In one approach, a parallel IGA and PSO developed and in other, a transitional approach of alternate IGA and PSO ...