的JavaScript(ES6),127个 126 87字节
f=(n,z=2,m=n*9+'',r=m.replace(/./g,1))=>n?m.length+(m<'55'?f(n- --r/10,0)-1:f(r-n,0)):z
Input: <input type="number" oninput="result.textContent=f(this.value)"> Result: <span id="result"></span>
应该工作到大约10 14 15,此时您开始遇到JavaScript的整数限制。说明:
f=( Recursive function
n, Parameter
z=2, Zero workaround
m=n*9+'', Magic
r=m.replace(/./g,1) Find repunit not less than than n
)=>n? Nothing to do if n is zero
m.length+ Assume subtracting from repunit
(m<'55'? Should we subtract from repunit?
f(n- --r/10,0) No, so subtract previous repuint
-1: Which is one 1 shorter
f(r-n,0)): Subtract from repunit
z Return special case if n is zero
这使用了n*9两次魔法。首先,它给我的下一个循环单位的长度,其次,如果前两位n*9是55或者更高,那么我们就需要减去n从下一个循环单位,否则我们需要减去1减去前一个循环单位(其计算和除以10)。这最多可以工作10 15。