|
用了第三次递归, 把结果整出来了。
上面的sql有个小问题, 最后一个序号没得到, 对于人工来说没问题, 只要知道最后一个数加上就行。
SQL不加处理的后面操作比较麻烦,所以稍作修改,与阶乘表用外连接关联, 从而取得第10个数
with targ as (select 1e6 arg1,9 arg2 from dual)
, prod(lastnum, lastprod) as
(select 0, 1 from dual
union all
select lastnum+1, (lastnum+1)*lastprod
from prod,targ
where lastnum < arg2
)
, perm(num, n, prd, d, m) as(
select arg1, 9, lastprod, 0, arg1 from targ,prod where lastnum=9
union all
select
num,
n-1,
lastprod,
floor((m-1)/prd),
mod(m-1,prd)+1
from perm,prod where n>=0 and lastnum(+)=n-1)
--select * from perm
,tseq as (select rownum x, d from perm where n<9 order by n desc)
, thelastdifficultstep (r, digi, str,path) as (
select 0, '', '0123456789', cast('' as varchar2(1000)) from dual
union all
select
r+1,
substr(str,d+1,1),
translate(str, '$'||path||substr(str,d+1,1), '$'),
path || substr(str,d+1,1)
from thelastdifficultstep,tseq where r<=10 and x=r+1)
--select * from thelastdifficultstep
select path from thelastdifficultstep where r=10
PATH
--------------------------------------------------------------------------------
2783915460
Executed in 0.297 seconds
第一次解决一个问题用三层递归。 脑袋快麻木了。
 |
|