前进两步,后退一步


15

假设我距离目的地十步之遥。我按照古老的谚语“向前走两步,向后退一步”走到那里。我向前走了两步,向后走了一步,直到我正好站在目的地上。(这可能涉及跨过我的目的地,然后返回目的地)。我走了几步?

当然,我可能还没有十步之遥。我可能只有11步之遥,或者是100步。我可以测量10个步伐,并继续来回走动以解决问题,或者...我可以编写一些代码!

  • 编写一个函数,计算出要走N步要走多少步,顺序为:向前两步,向后一步。
  • 假设您已从步骤0开始。将“向前两步”算作两步,而不是一步。
  • 假设所有步骤都是统一的长度。
  • 它应该返回到达该空间时首先执行的步骤数。(例如,距离10步需要26步,但是您在步骤30再次点击了它)。我们对26个感兴趣。
  • 使用您喜欢的任何语言。
  • 它应该接受任何正整数作为输入。这代表了目标步骤。
  • 最小的字节数获胜。

例:

我想走5步:

| | | | | | <- I'm at step 0, not yet on the grid.
| |X| | | | <- I take two steps forward, I'm on step 2: the count is 2
|X| | | | | <- I take one step back, I'm on step 1: the count is 3
| | |X| | | <- I take two steps forward, I'm on step 3: the count is 5
| |X| | | | <- I take one step back, I'm on step 2 again: the count is 6
| | | |X| | <- I take two steps forward, I'm on step 4: the count is 8
| | |X| | | <- I take one step back, I'm on step 3 again: the count is 9
| | | | |X| <- I take two steps forward, I'm on step 5: the count is 11

在这种情况下,该函数的结果将为11。

结果示例:

1      =>  3
5      =>  11
9      =>  23
10     =>  26
11     =>  29
100    =>  296
1000   =>  2996
10000  =>  29996
100000 =>  299996

玩得开心,高尔夫球手!


7
嗯...感觉很熟悉。
— 粗野的

3
— 杆

@Rod Hooray!我逃脱了!;)
— AJFaraday

是的,看起来像我在想的@Rod。
— 粗野的

@Shaggy Rod稍微改变了他的评论。较早的那个指出蜗牛/井的问题是在询问迭代的次数,但这是在询问覆盖的距离。
— AJFaraday

Answers:






9

多种语言:Java 8 / JavaScript / C#.NET,16 14 12字节

n->3*n-1%n*4

在线尝试(Java 8)。

n=>3*n-1%n*4

在线尝试(JavaScript)。
在线尝试(C#.NET)。

@Lynn的Python 2 Answer的端口,因此请确保对他/她的答案进行投票。


旧答案:

多种语言:Java 8 / JavaScript / C#.NET,16 14字节

n->n<2?3:n*3-4

在线尝试(Java 8)。

n=>n<2?3:n*3-4

在线尝试(JavaScript)。
在线尝试(C#.NET)。

说明:

n->       // Method with integer as both parameter and return-type
  n<2?    //  If the input is 1:
   3      //   Return 3
  :       //  Else:
   n*3-4  //   Return the input multiplied by 3, and subtract 4

JavaScript多语言(如果使用粗箭头)。
— 毛茸茸的

@Shaggy以及C#.NET :)虽然n=>(--n*3||4)-1在JavaScript中也是可行的(也是14字节)。
— 凯文·克鲁伊森

7

R,20个字节

N=scan();3*N-4*(N>1)

在线尝试!

直到我实施了不太优雅的解决方案后,才注意到这种模式。


3
恭喜10k BTW!
— 路易斯·门多

4
@LuisMendo谢谢!我认为我在网站上的一周年纪念日是几天前的,所以对我来说,这对PPCG来说是一个美好的一周。
— 朱塞佩

3
@Giuseppe我知道这种感觉:上周20k以及2周年纪念日。:)
— Kevin Cruijssen








4

MATL,7个字节

使用3*n-4*(n>1)公式。将输入乘以3(3*),再按一次输入(G)并递减(q)。如果结果不为零(?),则从结果(4-)中减去4 。

3*Gq?4-

在线尝试!


6个字节可移植Dennis的果冻答案2-|EG+
— 朱塞佩

4

果冻,4字节

ạ2Ḥ+

在线尝试!

怎么运行的

ạ2Ḥ+  Main link. Argument: n

ạ2    Absolute difference with 2; yield |n-2|.
  Ḥ   Unhalve/double; yield 2|n-2|.
   +  Add; yield 2|n-2|+n.



3

x86_64上的MachineCode,34 32 24字节

8d47fe9931d029d08d0447c3

需要i整数输出标志;输入是通过手动附加到代码来完成的。

在线尝试!


我通过以下4个不同的C函数来找到24字节的MachineCode程序:

  • n+2*abs(n-2) = 8d47fe9931d029d08d0447c3(24字节)
  • 3*n-4*!!~-n= 8d047f31d2ffcf0f95c2c1e20229d0c3(32字节)
  • n*3-4*(n>1)= 31d283ff028d047f0f9dc2c1e20229d0c3(34个字节)
  • n<2?3:n*3-4= 83ff01b8030000007e068d047f83e804c3(34个字节)

那么这是什么语言呢?
— qwr

@qwr查看存储库中的自述文件以获取简单描述。
— MD XF


2

4,54个字节

3.6010160303604047002020003100000180010202046000095024

在线尝试!

如果您对输入法有疑问,请首先访问数字输入和输出,该输入和输出可能会作为字符代码的元发布给出。


为什么这被否决?
— Uriel '18

因为答案似乎是四分之一,所以这不是有效的结果。据我所知,它不能解决问题。
— AJFaraday

@AJFaraday它使用字节输入和输出,通过元共识有效。参见输入部分的解释
— Uriel

Any resources on how to interpret the result? Or the input?
— AJFaraday

1
@AJFaraday the char code of the result is the answer. I've edited the question to include the relevant meta post. 4 has only char input.
— Uriel

2

Japt, 7 bytes

A port of Lynn's Python solution.

*3É%U*4

Try it


Alternative

This was a fun alternative to the closed formula solutions that is, unfortunately, a byte longer:

_+3}gN³²

Try it




2

65816 machine code, 22 bytes

我本可以很容易地使这65C02机器代码减少3个字节,但没有这样做,因为65C02上的寄存器大小是8位而不是16位。可以,但是很无聊,因为您只能使用非常低的数字;-)

xxd转储:

00000000: 7aa9 0000 aa89 0100 d004 8888 e824 c8e8  z............$..
00000010: 1ac0 0000 d0ef                           ......

反汇编/代码说明:

; target is on the stack
  ply              7A                  ; pull target from stack
  lda #$0000       A9 00 00            ; set loop counter to 0
  tax              AA                  ; set step counter to 0
loop:
  bit #$0001       89 01 00            ; sets Z if loop counter is even
  bne odd          D0 04               ; if Z is not set, jump to 'odd'
  dey              88                  ; decrement target twice
  dey              88
  inx              E8                  ; increment step counter
  .byte $24        24                  ; BIT $xx opcode, effectively skips the next byte
odd:
  iny              C8                  ; increment target

  inx              E8                  ; increment step counter
  inc a            1A                  ; increment loop counter

  cpy #$0000       C0 00 00            ; sets zero flag, can be optimized maybe?
  bne loop         D0 EF               ; if Y is non-zero, loop

; result is in register X

在65816兼容的仿真器上对其进行测试:

testing


1

外壳,28字节

F(){ bc<<<$1*3-$(($1>1))*4;}

测试:

F 1
3

F 2
2

F 3
5

F 4
8

F5
11

F 11
29

F 100
296

F 100000
299996

说明:

公式为:

if n == 1  ==> F(1) = 3
else F(n) = 3*n - 4

按照3步“前进两步,后退一步”的顺序,我们将获得算术级数:

 +2  2 => 2  ( or 6 )
 -1  1 => 3
 -----------
 +2  3 => 5  ( or 9 )
 -1  2 => 6
 -----------
 +2  4 => 8  ( or 12 )
 -1  3 => 9
 -----------
 +2  5 => 11 ( or 15 )
 -1  4 => 12
 -----------
 +2  6 => 14 ( or 18 )
 -1  5 => 15 
 -----------
 +2  7 => 17 ( or 21 )
 -1  6 => 18

至少或第一次巧合:

 1 => 3
 2 => 2
 3 => 5
 4 => 8
 5 => 11
 6 => 14

在一个公式中:

F(n) = 3*n - 4(n>1)     with n>1 is 1 or 0 (if n==1)

请描述这是哪个外壳
— qwr

在Cygwin上进行了测试(CYGWIN_NT-10.0 2.3.1(0.291 / 5/3)2015-11-14 12:44 x86_64 Cygwin)
— Ali ISSA

可以直接用BC写吗?
— qwr

我对bc不熟悉,但是由于函数($ 1)的参数已使用了几次,并且某些特定于shell的东西(算术扩展,$((…)))完成了,所以可能不这样做。
— 2xsaiko

1
F(){bc<<<$1*3-$(($1>1))*4} works in zsh though and removes 2 bytes
— 2xsaiko

1

Python 3, 48 bytes

def a(x):
    if x!=1:
        return((3*x)-4)
    return(3)

Try It Online!


Nice work. You might want to put some code in the “Footer” section, too. That way you can test your function without padding out your golf entry...
— AJFaraday

@AJFaraday The footer of my post or of my code?
— Nathan Dimmer

On Try It Online; you can add a footer which runs with your code but doesn’t count towards the byte length. Then the output will show your code at work.
— AJFaraday


@JoKing Do you know of a good guide to lambda functions in Python? I really don't understand how the syntax works.
— Nathan Dimmer



1

Brain-Flak, 38 bytes

({<([()()]{})>()(){(<((){})>)()}{}}{})

Try it online!

The first answer I see to calculate the answer by stepping back and forth.

({ while not at 0
  <([()()]{})>()() take two steps forward, counting 2 steps
  {(<((){})>)()}{} take one step back, if not at 0, and add 1 step
}{}) remove the 0 and push step sum

1

W d, 7 bytes

♦óÖ╣░Θ$

Explanation

3*1a<4*-

Evaluates (a*3)-4*(a>1).

Another possible alternative

3*1am4*-

Evaluates (a*3)-4*(1%a).

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.