免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1986 | 回复: 2

[其他] Project Euler - 008 [复制链接]

论坛徽章:
4
白羊座
日期:2013-11-05 10:26:09冥斗士
日期:2015-11-17 14:19:55白银圣斗士
日期:2015-11-17 15:13:0815-16赛季CBA联赛之新疆
日期:2016-04-01 09:10:58
发表于 2015-09-27 17:49 |显示全部楼层
Problem 8:
The four adjacent digits in the 1000-digit number that have the greatest product are 9 × 9 × 8 × 9 = 5832.

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

Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?

问题8:
下列1000个数字中,有最大乘积的四个相邻数字是:9 * 9 * 8 * 9 = 5832

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

试找出上面1000个数字中有最大乘积的相邻的13个数字。同时,乘积是多少?

代码:

  1. package main

  2. import (
  3.         "fmt"
  4.         "strings"
  5. )

  6. func SequenceProduct(data [] int ) int {
  7.         product := 1
  8.         for index := 0; index < len(data); index++ {
  9.                 product *= data[index]
  10.         }
  11.         return product
  12. }

  13. func FindMaxSequenceOfProduct(data []int, length int) (int , [] int) {
  14.         position := 0
  15.         baseProduct := 1
  16.         // Initialize the product for checking
  17.         for index := 0; index < length; index++ {
  18.                 baseProduct = baseProduct * data[index]
  19.         }
  20.        
  21.         product := baseProduct
  22.         for index := length; index < len(data); index++ {
  23.                 product = product / data[index - length] * data[index]
  24.                 if product > baseProduct {
  25.                         baseProduct = product
  26.                         position = index - length + 1
  27.                 }
  28.         }
  29.        
  30.         return SequenceProduct(data[position:position + length]), data[position:position + length]
  31. }

  32. func Problem008(content string, length int) ([]int, int) {
  33.         // Step 1, split with 0
  34.         sequences := strings.Split(content, "0")

  35.         produce := 0
  36.         maxSequence := make([]int, 0)
  37.         // Step 2, for each of the item lenth >= length limit
  38.         for _, sequence := range sequences {
  39.                 if len(sequence) >= length {
  40.                         data := make([]int , 0)
  41.                         for _, item := range sequence {
  42.                                 data = append(data, int(item - 48))
  43.                         }
  44.                         curProduce, curSequence := FindMaxSequenceOfProduct(data, length)
  45.                         if curProduce > produce {
  46.                                 maxSequence = curSequence
  47.                                 produce = curProduce
  48.                         }
  49.                 }
  50.         }
  51.        
  52.         return maxSequence, SequenceProduct(maxSequence)
  53. }

  54. func main() {
  55.         content := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
  56.         sequence, product := Problem008(content, 13)
  57.         fmt.Println("Problem 008 result: ", sequence, product)
  58. }
复制代码

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
发表于 2015-12-03 14:34 |显示全部楼层
回复 1# icymirror

显示
Problem 008 result:  [8 6 1 7 8 6 6 4 5 8 3 5 9] 2090188800

这个本身

[5 5 7 6 6 8 9 6 6 4 8 9 5] 23514624000

23514624000 > 2090188800

12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113

程序如何修改
谢谢

论坛徽章:
6
数据库技术版块每日发帖之星
日期:2015-11-27 06:20:00程序设计版块每日发帖之星
日期:2015-12-01 06:20:00每日论坛发贴之星
日期:2015-12-01 06:20:0015-16赛季CBA联赛之佛山
日期:2017-03-26 23:38:0315-16赛季CBA联赛之江苏
日期:2017-07-17 10:08:4415-16赛季CBA联赛之北京
日期:2018-03-04 17:01:50
发表于 2015-12-06 14:03 |显示全部楼层
有最大乘积
[5 5 7 6 6 8 9 6 6 4 8 9 5] 23514624000
  1. // You can edit this code!
  2. // Click here and start typing.
  3. package main

  4. import "fmt"

  5. func main() {
  6.         num := 13
  7.         str := "7316717653133062491922511967442657474235534919493496983520312774506326239578318016984801869478851843858615607891129494954595017379583319528532088055111254069874715852386305071569329096329522744304355766896648950445244523161731856403098711121722383113622298934233803081353362766142828064444866452387493035890729629049156044077239071381051585930796086670172427121883998797908792274921901699720888093776657273330010533678812202354218097512545405947522435258490771167055601360483958644670632441572215539753697817977846174064955149290862569321978468622482839722413756570560574902614079729686524145351004748216637048440319989000889524345065854122758866688116427171479924442928230863465674813919123162824586178664583591245665294765456828489128831426076900422421902267105562632111110937054421750694165896040807198403850962455444362981230987879927244284909188845801561660979191338754992005240636899125607176060588611646710940507754100225698315520005593572972571636269561882670428252483600823257530420752963450"
  8.         arr := make([]int64, len(str))
  9.         for i, e := range str {
  10.                 arr[i] = int64(e - 48)
  11.         }

  12.         var max int64 = 0
  13.         index := 0
  14.         for i := 0; i < len(arr)-num; i++ {
  15.                 var val int64 = 1
  16.                 for _, e := range arr[i : i+num] {
  17.                         val *= e

  18.                 }
  19.                 if val > max {
  20.                         max = val
  21.                         index = i
  22.                 }
  23.         }
  24.         fmt.Println(arr[index:index+num], max)
  25. }
复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP