环绕我,救救我


23

给定输入后n,您的程序或函数必须输出最小的正整数k,以便n四舍五入到的最接近倍数k大于n

例。

给定输入20,输出值应为3

  • 最接近的倍数1就是20,这是大于20

  • 最接近的倍数2就是20,这是大于20

  • 最接近的倍数3就是21,这大于20,所以它是输出。

测试用例

#Input  #Output
2       3
4       5
6       4
8       3
10      4
12      7
14      3
16      6
18      4
20      3
22      4
24      5
26      3
28      5
30      4
32      3
34      4
36      8
38      3
40      6
42      4
44      3
46      4
48      5
50      3
52      6
54      4
56      3
58      4
60      7
62      3
64      5
66      4
68      3
70      4
72      11
74      3
76      6
78      4
80      3
82      4
84      5
86      3
88      5
90      4
92      3
94      4
96      7
98      3
1000    6

给定任何奇数输入的输出应为2。

规则

  • n 是小于的正整数 2^32
  • 进行四舍五入,以使得如果的两个倍数k相等地相距n,则选择更大的一个(“四舍五入”)。这样,每个奇数都会n产生的输出2
  • 这是,因此每种语言中最短的代码将获胜。

我已经对测试用例的格式进行了编辑,以使其更易于阅读和简洁。让我知道您是否对此有任何疑问,或者是否有任何新示例不可用。:)
DJMcMayhem

@蓬松的完成!我从列表中删除了500赔率和450偶数。
fireflame241

这个序列有一个oeis链接吗?
James K

@JamesK我之前搜索时没有找到。拥有OEIS帐户的人可能可以创建一个吗?
fireflame241

Answers:



9

Japt,6个字节

@<rX}a

在线尝试!

说明:

@    <r X}a
XYZ{U<UrX}a
X              // X = 0; Increments when the condition in between {...} fails
   {     }a    // Return the first integer X where:
    U          //   The input
     <U        //   is less than the input
       rX      //     rounded to the nearest multiple of X


@EriktheOutgolfer:Japt还具有用于向上或向下取整的内置方法:)
Shaggy的

5
知道这一功能会在某天派上用场:D
ETHproductions'Aug

@Shaggy真是疯了!o_o_o
埃里克(Erik the Outgolfer)

@Oliver:这让我更加坚信要掌握函数方法,现在-我自己的函数版本是7个字节:o æ@<rX
Shaggy

7

MATL,13字节

tQ:yy/Yo*<fX<

在线尝试!验证从1到的所有输入1000

说明

考虑输入6

t      % Implicit input. Duplicate
       % STACK: 6, 6
Q:     % Add 1, range
       % STACK: 6, [1 2 3 4 5 6 7]
yy     % Duplicate top two elements
       % STACK: 6, [1 2 3 4 5 6 7], 6, [1 2 3 4 5 6 7]
/      % Divide, element-wise
       % STACK: 6, [1 2 3 4 5 6 7], [6 3 2 1.5 1.2 1 0.8571]
Yo     % Round to closest integer. Halves are rounded up
       % STACK: 6, [1 2 3 4 5 6 7], [6 3 2 2 1 1 1]
*      % Multiply, element-wise
       % STACK: 6, [6 6 6 8 5 6 7]
<      % Less than, element-wise
       % STACK: [0 0 0 1 0 0 1]
f      % Find: indices of nonzeros (1-based)
       % STACK: [4 7]
X<     % Minimum of vector. Implicit display
       % STACK: 4


5

JavaScript(ES6),28 25字节

n=>g=x=>n%x>=x/2?x:g(-~x)
  • 感谢Arnauld,节省了3个字节。

测试一下

o.innerText=(f=

n=>g=x=>n%x>=x/2?x:g(-~x)

)(i.value=64)();oninput=_=>o.innerText=f(+i.value)()
<input id=i type=number><pre id=o>

或测试1-1000中的所有数字(请稍候运行):


5

质子,33字节

n=>[x for x:2..n+2if n%x>=x/2][0]

在线尝试!


我对Proton一无所知,但是看来您可以节省3个字节:在线试用!
jferard '17

也许是一个巧合,但这与完全人类的解决方案完全相同...:p
暴民埃里克(Erik the Outgolfer)

@EriktheOutgolfer我们在同一时间(实际上是我忍耐了他几秒钟)以37-byter发布了它,因为Hyper使操作员感到厌烦,当他修复他们时,我们都进行了更新。
Xcoder先生17年

嗯,我为你IIRC了。:P
totallyhuman

@totallyhuman您以41岁的忍者向我求婚。我先张贴了37人的影片,然后用几秒钟忍住了你。
Xcoder先生17年



3

果冻,11 字节

÷R%1<.¬;1TṂ

取并返回正整数的单子链接。

在线尝试!或查看测试套件

怎么样?

÷R%1<.¬;1TṂ - Link: number, n       e.g. 10
 R          - range(n)               [ 1,2,3     ,4  ,5,6     ,7     ,8   ,9     ,10]
÷           - n divided by           [10,5,3.33..,2.5,2,1.66..,1.42..,1.25,1.11..,1 ]
  %1        - modulo by 1            [ 0,0,0.33..,0.5,0,0.66..,0.42..,0.25,0.11..,0 ]
    <.      - less than 0.5?         [ 1,1,1     ,0  ,1,0     ,1     ,1   ,1     ,1 ]
      ¬     - not                    [ 0,0,0     ,1  ,0,1     ,0     ,0   ,0     ,0 ]
       ;1   - concatenate a 1        [ 0,0,0     ,1  ,0,1     ,0     ,0   ,0     ,0 , 1]
         T  - truthy indices         [            4    ,6                           ,11]
          Ṃ - minimum                4

注:级联1只是处理情况n是一个12或者4当结果需要是n+1‘R÷@%1<.¬TṂ也将工作)。




2

Pyth,5个字节

fgy%Q

测试套件

没有舍入内建函数,仅检查第一个正整数T,其中输入mod T的两倍大于或等于T。

说明:

fgy%Q
fgy%QTT    Implicit variable introduction.
f          Find the first positive integer T such that the following is truthy:
   %QT     Input % T
  y        Doubled
 g    T    Is greater than or equal to T

2

x86机器代码,17个字节

这段代码以可重用函数的形式实现了基本的迭代解决方案:

31 F6                   xor    esi, esi
46                      inc    esi         ; set ESI (our temp register) to 1

                     Loop:
89 C8                   mov    eax, ecx    ; copy 'n' to EAX for division
46                      inc    esi         ; eagerly increment temp
99                      cdq                ; extend EAX into EDX:EAX
F7 F6                   div    esi         ; divide EDX:EAX by ESI
01 D2                   add    edx, edx    ; multiply remainder by 2
39 F2                   cmp    edx, esi    ; compare remainder*2 to temp
7C F4                   jb     Loop        ; keep looping if remainder*2 < temp

96                      xchg   eax, esi    ; put result into EAX (1 byte shorter than MOV)
C3                      ret

该函数遵循fastcall调用约定,因此单个参数(n)在ECX寄存器中传递。k通常,返回值()在EAX寄存器中返回。

在线尝试!


2

Java 8,42字节

Lambda从IntegerInteger

n->{for(int f=1;;)if(n%++f*2>=f)return f;}

在线试用

致谢

  • -1字节感谢Kevin Cruijssen

4
您可以f=1通过++f在第一个上启动并使用来保存字节f,如下所示:n->{for(int f=1;;)if(n%++f*2>=f)return f;}
Kevin Cruijssen


1

第四(gforth),45字节

: f 1 begin 1+ 2dup mod over 1+ 2/ >= until ;

在线尝试!

代码说明

: f             \ start a new word definition
  1             \ start a counter at 1
  begin         \ start an indefinite loop
    1+          \ add 1 to counter
    2dup mod    \ duplicate input value and counter, get remainder of input/counter
    over 1+ 2/  \ get counter/2 (add 1 to force rounding up)
    >=          \ check if remainder is greater than counter/2
  until         \ end loop if true, otherwise go back to beginning
;               \ end word definition

1

05AB1E,9个字节

∞.ΔIs/Dò‹

在线尝试!

说明

∞.ΔIs/Dò‹ Full code
∞.Δ       Returns the first number for which the following code returns true
             -> stack is [n]
   Is     Push the input and swap the stack -> stack is [input, n]
     /    Divide both of them -> stack is [input/n]
      Dò  Duplicate and round the second -> stack is [input/n, rounded(input/n)]
        ‹ Check if input/n got larger by rounding -> stack is [bool]
             -> if bool is true, abort and return the current number

1

摇滚明星,681字节

Thought takes Patience and Control
While Patience is as high as Control
Let Patience be without Control

Give back Patience

Rock takes Art
Love is neverending
Sex is bottomless
Put Thought taking Art & Love into your head
If your head is Sex
Give back Art
Else
Limits are inspiration
Put Art with Limits without your head into the rubbish
Give back the rubbish


Listen to Chance
Questions are unstoppable
Until Questions is Chance
Build Questions up
Put Thought taking Chance, Questions into your mind
Answers are independence (but)
Put Questions over Answers into the world
Put Rock taking the world into the world
If your mind is as big as the world
Say Questions
Break it down

您可以在线试用rockstar,但是需要复制并粘贴代码。它将提示您输入数字。

我并没有追求最低的字节数,因为Rockstar显然不是为打高尔夫球而设计的,因此我尝试使用Rock'n'Roll歌词。

说明:

这是基于与其他解决方案(python,java)相同的解决方案的:

Iterate up from 2:
if n % iterator >= ceil(n/2)
    return iterator

首先,我需要定义模数和上限函数,就诗歌而言,它们被称为思想和摇滚。

下面是一个不太诗意的版本,具有不同的变量名,并在语法不清楚的地方进行了说明。括号表示注释。

Modulus takes Number and Divisor
While Number is as high as Divisor
Put Number minus Divisor into Number
    (blank line ending While block)
Give back Number (return Number)
    (blank line ending function declaration)
Ceil takes Decimal
Put Modulus taking Decimal, 1 into Remainder
If Remainder is 0
Give back Decimal (return Decimal)
Else
Put Decimal with 1 minus Remainder into Result
Give back Result (return Result)
    (blank line ending if block)
    (blank line ending function declaration)
Listen to Input (Read from STDIN to Input)
Index is 1
Until Index is Input
Build Index up (Increment by 1)
Put Modulus taking Input, Index into LHS
Put Index over 2 into RHS
Put Ceil taking RHS into RHS
If LHS is as big as RHS
Say Index
Break it down (Break from loop)




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.