Oracle SQL,456字节
select listagg((select listagg(l)within group(order by 1)from dual start with trunc((n-nvl(n-mod(n,p),0))/v)>0 connect by level<=trunc((n-nvl(n-mod(n,p),0))/v)))within group(order by v desc)from (select 2849n from dual)cross join(select 1000v,null p,'m'l from dual union select 500,1000,'d'from dual union select 100,500,'c'from dual union select 50,100,'l'from dual union select 10,50,'x'from dual union select 5,10,'v'from dual union select 1,5,'i'from dual)
输出:
mmdcccxxxxviiii
请注意,该行的实际大小为460字节,因为它包括输入数字(2849)。
取消高尔夫:
select listagg(
(select listagg(l, '') within group(order by 1)
from dual
start with trunc((n-nvl(p*trunc(n/p),0))/v) > 0
connect by level <= trunc((n-nvl(p*trunc(n/p),0))/v) )
) within group(order by v desc)
from (select 2348 n
from dual
) cross join (
select 1000v, null p, 'm' l from dual union
select 500, 1000, 'd' from dual union
select 100, 500, 'c' from dual union
select 50, 100, 'l' from dual union
select 10, 50, 'x' from dual union
select 5, 10, 'v' from dual union
select 1, 5, 'i' from dual
)
它是如何工作的:我计算出每个字母需要多少个字母,方法是计算一个最大的字母(M的无穷大),然后在当前字母的值与该字母的结果之间进行整数除法。
例如2348,C
我需要几个?trunc((2348-mod(2348,500))/100)
= 3。
然后,我listagg
将这封信放在一起3次(利用CONNECT BY
该行生成我需要的3行)。最后,我listagg
一切都在一起。
有点大,但是大多数是select from dual
转换表中的s,我对此真的不能做太多...
4 -> IIII
是9 -> VIIII
也,而不是IX
?