level 11
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.
2014年03月28日 04点03分
1
level 11
此题找回文数还是很简单的只要简单的从头尾匹配即可。
附加题:在一串字母中找出 最长回文串 。你的最好算法效率如何?
2014年03月31日 12点03分
2
level 11
不含附加题。
;每日一题004 最大回文数乘积
(require "../mod/constrmod.rkt")
;-----------------
;判断回文数
(define (palindromic-num? num)
(let* ((vectnum (list->vector (string->listnum (number->string num))))
(len (vector-length vectnum))
(testlen 0))
(if (even? len)
(set! testlen (/ len 2))
(set! testlen (/ (sub1 len) 2)))
(let loop ((i 0))
(cond ((= i testlen) #t)
((= (vector-ref vectnum i)
(vector-ref vectnum (- len i 1)))
(loop (add1 i)))
(else
#f)))))
;n位乘积构成的回文数
(define (n-digit-hwnum n)
(let ((result '()))
(do ((i (expt 10 (sub1 n)) (add1 i)))
((> i (sub1 (expt 10 n))) (sort result #:key car >))
(do ((j i (add1 j)))
((> j (sub1 (expt 10 n))))
(let ((r (* i j)))
(when (palindromic-num? r)
(set! result (cons (list r i j) result))))))))
;(map (lambda (x) (displayln (n-digit-hwnum x))) '(1 2 3))
(car (n-digit-hwnum 3))
;'(906609 913 993)
2014年03月31日 12点03分
3