November 20 2015

巧用with排列组合

用字母A,B,C,D和E,以字母顺序构成25字符的代码,这样的代码总共有多少个?

如果问的是用A,B和C构成4个字母的代码,答案是15:

AAAA, AAAB, AAAC, AABB, AABC, AACC, ABBB, ABBC, ABCC, ACCC, BBBB, BBBC, BBCC, BCCC, CCCC.

 

with t as (select chr(level+64) c from dual connect by level<=5),
s(lvl,str) as (select 1 lvl,c str
from t
union all
select s.lvl +1,
s.str||t.c
from s,t
where lvl < 25
and substr(s.str,length(s.str),1)<=t.c
)
select count(*) from s where lvl = 25



Copyright 2019. All rights reserved.

Posted 2015年11月20日 by ucjmh in category "oracle

Leave a Reply

Your email address will not be published. Required fields are marked *