【每日一题024】Lexicographic permutations
drracket吧
全部回复
仅看楼主
level 11
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
012 021 102 120 201 210
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
2014年04月16日 06点04分 1
level 11
#lang racket
;;;每日一题024
;;;生成组合数
(define (combination lst end-n)
(define tree-lst '())
(define count 0)
(let/cc call
(let make-tree-sbs ((lst lst))
(cond ((null? lst)
(set! count (add1 count))
(printf "No.~a,\t~a.\n" count (reverse tree-lst))
(when (= count end-n) (call (format "No.~a,~a." count (reverse tree-lst))))
(set! tree-lst (cdr tree-lst))
null)
(else
(let loop ((l lst))
(cond ((null? l) (unless (null? tree-lst) (set! tree-lst (cdr tree-lst))) null)
(else
(let* ((x (car l)))
(set! tree-lst (cons x tree-lst))
(cons (cons x (make-tree-sbs (remove x lst)))
(loop (cdr l)))))
)))))))
(combination '(1 2 3 4) 10)
;(combination '(0 1 2 3 4 5 6 7 8 9) 10)
(combination '(0 1 2 3 4 5 6 7 8 9) (expt 10 6))
#|
No.999995,(2 7 8 3 9 1 4 6 0 5).
No.999996,(2 7 8 3 9 1 4 6 5 0).
No.999997,(2 7 8 3 9 1 5 0 4 6).
No.999998,(2 7 8 3 9 1 5 0 6 4).
No.999999,(2 7 8 3 9 1 5 4 0 6).
No.1000000,(2 7 8 3 9 1 5 4 6 0).
"No.1000000,(2 7 8 3 9 1 5 4 6 0)."
|#
2014年05月01日 14点05分 3
level 11
(define (combination2 lst end-n)
.....
(else
(let* ((x (car l)))
(set! tree-lst (cons x tree-lst))
(make-tree-sbs (remove x lst)) ;修改 目的不是生成树,所以不需要占用内存存贮树结构,只要产生树过程就行了。
(loop (cdr l))))
)))))))
(combination2 '(1 2 3 4) -1)
(combination2 '(0 1 2 3 4 5 6 7 8 9) -1)
2014年05月02日 02点05分 4
1