产生惰性值


25

相关:对我的微波炉编程。受生成懒惰微波输入的启发。

非负整数N的惰性值是最接近N的整数中最小的,而它们的所有数字都相同。

返回(通过任何手段)的给定(通过任何手段)懒惰值Ñ

ñ你的语言代表了非指数形式默认的最大整数。1000000(由于此过高的要求,许多有趣的解决方案丢失了。)

测试用例:

   0 →    0
   8 →    8
   9 →    9
  10 →    9
  16 →   11
  17 →   22
  27 →   22
  28 →   33
 100 →   99
 105 →   99
 106 →  111
 610 →  555
 611 →  666
7221 → 6666
7222 → 7777 

有问题的同事证明不存在联系:除了9 / 11、99 / 111等(一个比另一个短)以外,两个连续的有效答案总是相距一个奇数距离,因此没有整数可以精确地与他们等距。

Answers:


15

JavaScript(ES6),31个字节

n=>~-(n*9+4).toPrecision(1)/9|0

直接计算每个的惰性值n

编辑:由于JavaScript整数类型的限制,最多只能运行277777778。替代版本:

n=>((n*9+4).toPrecision(1)-1)/9>>>0

35个字节,最多可处理16666666667。

n=>((n=(n*9+4).toPrecision(1))-n[0])/9

38个字节,最多可使用944444444444443。但这仍比2 53(9007199254740992)少了一些。


@ user81655我添加了一些具有数字限制的替代版本。
尼尔

1
我无法使用此算法,Number.MAX_SAFE_INTEGER因为8e16 - 1它表示为8e16。可悲的是,看起来唯一的方法就是对最大结果进行硬编码。+1。
user81655 '16

@ user81655我降低了上限以允许解决方案。
亚当

让您达到10k @Neil,喜欢高尔夫球!
NiCk Newman

1
@NiCkNewman呜呼!谢谢!
尼尔

5

果冻,16个字节

ḤRµDIASµÐḟµạ³ỤḢị

在线尝试!

怎么运行的

ḤRµDIASµÐḟµạ³ỤḢị  Main link. Input: n

Ḥ                 Compute 2n.
 R                Yield [1, ..., 2n] or [0].
  µ               Begin a new, monadic chain. Argument: R (range)
   D              Convert to base 10.
    I             Compute all differences of consecutive decimal digits.
     A            Take the absolute values of the differences.
      S           Sum the absolute values.
       µÐḟ        Filter-false by the chain to the left.
          µ       Begin a new, monadic chain. Argument: L (lazy integers)
           ạ³     Take the absolute difference of each lazy integer and n (input).
             Ụ    Grade up; sort the indices of L by the absolute differences.
                  This is stable, so ties are broken by earlier occurrence and,
                  therefore, lower value.
              Ḣ   Head; retrieve the first index, corresponding to the lowest
                  absolute difference.
               ị  Retrieve the item of L at that index.

4

Oracle SQL 11.2,200字节

WITH v(i)AS(SELECT 0 FROM DUAL UNION ALL SELECT DECODE(SIGN(i),0,-1,-1,-i,-i-1)FROM v WHERE LENGTH(REGEXP_REPLACE(:1+i,'([0-9])\1+','\1'))>1)SELECT:1+MIN(i)KEEP(DENSE_RANK LAST ORDER BY rownum)FROM v;

未打高尔夫球

WITH v(i) AS
(
  SELECT 0 FROM DUAL      -- Starts with 0
  UNION ALL
  SELECT DECODE(SIGN(i),0,-1,-1,-i,-i-1) -- Increments i, alternating between negatives and positives
  FROM   v 
  WHERE  LENGTH(REGEXP_REPLACE(:1+i,'([0-9])\1+','\1'))>1  -- Stop when the numbers is composed of only one digit
)
SELECT :1+MIN(i)KEEP(DENSE_RANK LAST ORDER BY rownum) FROM v;

3

Pyth-26个字节

这个答案并不总是在平局中返回最小值,但是在规范中却没有,因此请等待 3个字节的澄清

hSh.g.a-kQsmsM*RdjkUTtBl`Q

测试套件


3

Pyth,16个字节

haDQsM*M*`MTSl`Q

在线尝试:演示测试套件

说明:

haDQsM*M*`MTSl`Q   implicit: Q = input number
              `Q   convert Q to a string
             l     take the length
            S      create the list [1, 2, ..., len(str(Q))]
         `MT       create the list ["0", "1", "2", "3", ..., "9"]
        *          create every combination of these two lists:
                   [[1, "0"], [1, "1"], [1, "2"], ..., [len(str(Q)), "9"]]
      *M           repeat the second char of each pair according to the number:
                   ["0", "1", "2", ..., "9...9"]
    sM             convert each string to a number [0, 1, 2, ..., 9...9]
  D                order these numbers by:
 a Q                  their absolute difference with Q
h                  print the first one

3

MATL,25个字节

2*:"@Vt!=?@]]N$vtG-|4#X<)

使用蛮力,因此大量可能需要一段时间。

在线尝试!

2*:       % range [1,2,...,2*N], where is input
"         % for each number in that range
  @V      %   push that number, convert to string
  t!=     %   test all pair-wise combinations of digits for equality
  ?       %   if they are all equal
    @     %     push number: it's a valid candidate
  ]       %   end if
]         % end for each
N$v       % column array of all stack contents, that is, all candidate numbers
t         % duplicate
G-|       % absolute difference of each candidate with respect to input
4#X<      % arg min
)         % index into candidate array to obtain the minimizer. Implicitly display

3

Perl,32岁

基于Neil精美的JavaScript解决方案。

$_=0|1/9*~-sprintf"%.e",$_*9+4.1

在以下位置开始失败 5e15


2

Mathematica,122个字节

f@x_:=Last@Sort[Flatten@Table[y*z,{y,1,9},{z,{FromDigits@Table[1,10~Log~x+1-Log[10,1055555]~Mod~1]}}],Abs[x-#]>Abs[x-#2]&]

函数名为x。


2

JavaScript(ES6),59个字节

n=>eval(`for(i=a=0;i<=n;a=i%10?a:++i)p=i,i+=a;n-p>i-n?i:p`)

递归解决方案(56字节)

这有点短,但n > 1111111110由于超过了最大调用堆栈大小而无法使用,因此从技术上讲是无效的。

f=(n,p,a,i=0)=>n<i?n-p>i-n?i:p:f(n,i,(i-=~a)%10?a:i++,i)

说明

遍历每个惰性数字,直到到达大于的第一个惰性数字n,然后将n其与前一个数字进行比较以确定结果。

var solution =

n=>
  eval(`           // eval enables for loop without {} or return
    for(
      i=a=0;       // initialise i and a to 0
      i<=n;        // loop until i > n, '<=' saves having to declare p above
      a=i%10?a:++i // a = amount to increment i each iteration, if i % 10 == 0 (eg.
    )              //     99 + 11 = 110), increment i and set a to i (both become 111)
      p=i,         // set p before incrementing i
      i+=a;        // add the increment amount to i
    n-p>i-n?i:p    // return the closer value of i or p
  `)
N = <input type="number" oninput="R.textContent=solution(+this.value)"><pre id="R"></pre>


我降低了上限,以允许您解决。
亚当


1

05AB1E,20个字节

9Ývy7L×})˜ïD¹-ÄWQÏ{¬

在线尝试!

9Ý                   # Push 0..9
  vy7L×})˜           # For each digit, 0-9, push 1-7 copies of that number.
          ïD         # Convert to integers, dupe the list.
            ¹        # Push original input (n).
             -Ä      # Push absolute differences.
               WQ    # Get min, push 1 for min indices.
                 Ï{¬ # Push indices from original array that are the min, sort, take first.

99肯定比111更懒,因为它只需要按两次按钮即可。
亚当

@Adám足够公平,增加了总指挥权。
Magic Octopus Urn

1

Mathematica,56个字节

Min@Nearest[##&@@@Table[d(10^n-1)/9,{n,0,6},{d,0,9}],#]&

具有第一个参数的纯函数 #,适用于最高输入的输入10^6

对于非负整数n和数字d10^n-1 = 99...99重复n次数),所以d(10^n-1)/9 = dd...dd重复n次数)。创建一个Table值的0 <= n <= 60 <= d <= 9,然后变平表,找出元素列表Nearest#并采取Min

我相信这个版本将适用于任意大整数:

Min@Nearest[##&@@@Table[d(10^n-1)/9,{n,0,IntegerLength@#},{d,0,9}],#]&
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.