免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 40230 | 回复: 3
打印 上一主题 下一主题

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

论坛徽章:
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
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2015-09-26 17:45 |只看该作者 |倒序浏览
本帖最后由 icymirror 于 2015-09-26 17:49 编辑

Problem 4:
A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 × 99.
Find the largest palindrome made from the product of two 3-digit numbers.

问题4:
一个回文数从两头读是相同的。由两位数相乘得到的最大回文数是:9009=91*99。
试找出由三位数相乘得到的最大回文数。

代码:
  1. package main

  2. import (
  3.         "math"
  4.         "strconv"
  5. )

  6. func IsPalindrome(number int) bool {
  7.         // Function could only check number no more than 5 digits
  8.         // Initialize
  9.         result := true
  10.         content := strconv.Itoa(number)
  11.         length := len(content)
  12.        
  13.         // Checking whether number is palindrome
  14.         for position := 0; position < length / 2; position++ {
  15.                 if content[position] != content[length - position - 1] {
  16.                         result = false
  17.                         break
  18.                 }
  19.         }
  20.        
  21.         return result
  22. }

  23. func Problem004() int {
  24.         result := 0
  25.        
  26.         for index := 100; index < 1000; index++ {
  27.                 for subIndex := index; subIndex < 1000; subIndex++ {
  28.                         testNumber := index * subIndex
  29.                         if (IsPalindrome(testNumber) && testNumber > result) {
  30.                                 result = index * subIndex
  31.                         }
  32.                 }
  33.         }
  34.        
  35.         return result
  36. }

  37. func main() {
  38.         fmt.Println("Problem 004 result: ", Problem004())
  39. }
复制代码

论坛徽章:
0
2 [报告]
发表于 2015-10-10 13:04 |只看该作者
本帖最后由 ba_du_co 于 2015-10-10 13:05 编辑

拿来练练手。
906609
  1. #!perl6

  2. sub mapali ($n) {
  3.     my ( $a, $b ) = 10 ** ( $n - 1 ), 10 ** $n;
  4.     [max] ($a ..^ $b).map: -> $x {
  5.     [max] ($x ..^ $b).map: -> $y {
  6.         my int $p = $x * $y;
  7.         $p if $p eq $p.flip;
  8. } } }

  9. say mapali 3;
复制代码

论坛徽章:
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
3 [报告]
发表于 2017-03-21 13:39 |只看该作者
欢迎大家发帖讨论,分享是美德
  1. package main

  2. func 回文(数值 int) bool {
  3.         回文数 := 0
  4.         for 这个 := 数值; 这个 > 0; 这个 /= 10 {
  5.                 回文数 = 回文数*10 + 这个%10
  6.         }
  7.         return 回文数 == 数值
  8. }

  9. func 问题4() {
  10.         最小, 最大, 最大回文数 := 100, 999, 0

  11.         for 当前 := 最大; 当前 >= 最小; 当前-- {
  12.                 for 到达 := 当前; 到达 >= 最小; 到达-- {
  13.                         这个 := 当前 * 到达
  14.                         if 这个 <= 最大回文数 { break }
  15.                         if 回文(这个) { 最大回文数, 最小 = 这个, 这个/当前 }
  16.                 }
  17.         }
  18.         println("由三位数相乘得到的最大回文数是:", 最大回文数)
  19. }

  20. func main() { 问题4() }

复制代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP