向下数字竞赛


10

您的任务是创建一个程序,该程序将随机数字添加到最终数字竞赛摊牌中的先前总和。

每个赛车手(列)从0开始,并在比赛的每一步中将1或0添加到先前的总和,直到所有赛车手达到获胜所需的分数为止。1或0应该随机选择(可以在此处找到random的标准定义)。输出将显示比赛的结果,每一列以一种格式表示:

>> racers:5,score needed:2

0 0 0 0 0 # all racers start at 0
+ + + + + # add
1 0 0 0 1 # random 1 or 0
= = = = = # equals
1 0 0 0 1 # sum
+ + + + +
0 0 0 0 1
= = = = =
1 0 0 0 2 # winner!
+ + + +  
1 1 1 1  
= = = =  
2 1 1 1  
  + + +  
  1 1 1  
  = = =  
  2 2 2   # losers

注意:输出中只需要包含数字+和=。

输入值

您的程序将接受以下两个参数作为输入:

  1. 赛车手(列)的数量,必须大于两个
  2. 获胜所需的分数,必须大于1

这是代码高尔夫球-字节最少的程序获胜。

编辑:有一个不可执行的最高分9-这是为了保持列的完整性。同样,在输出中可以省略列之间的空格。


需要支持的最大列数和最大分数是多少?
nanofarad

没有定义的最大值,因此它将与最小值相同:至少三列且得分为两。
气象学家,2016年

3
要求的分数会有两位数字吗?
Leaky Nun

4
“仅数字,+和=需要包含在输出中。” 那空间呢?
Leaky Nun

不需要保留空格,为了清楚起见,在示例中保留了空格。关于两位数的好问题-我想有一个最高分,就是9。我将编辑问题。
气象学家,2016年

Answers:


5

果冻,37 36 33字节

,‘X
0Ç<³$пµżIFµ“+=”ṁṖ⁸żF
ÇСḊz⁶G

3字节感谢丹尼斯。

在线尝试

说明

,‘X                    Helper link. Argument: n. Radomly return n or n+1.

 ‘                     Increment n
,                      Pair. Yield [n, n+1]
  X                    Return a random item from the pair.

0Ç<³$пµżIFµ“+=”ṁṖ⁸żF   Monadic link. Argument: s. Generate one racer.

0                       Start with value 0.
  <³$пµ                While value is less than s:
 Ç                        Use helper link to increment current value.
                        Collect intermediate results in a list.
         I              Compute consecutive differences.
        ż               Zip intermediate results with their next increment value 0 or 1.
          Fµ            Flatten. Let's call the current list A.
                        Odd items of A are racer state and even items are random 0 or 1.
            “+=”        Yield "+=".
                 Ṗ      Yield A without its last element.
                ṁ       Mold i.e Repeat the characters of the string until it contains length(A)-1 characters.
                  ⁸ż    Zipwith. Pair the elements of A with the correponding characters
                    F   Flatten.

ÇСṫ2z” G               Main link. Arguments: s (score needed), r (#racers)

ÇС                     Call the link above r times.
                        Generate a list of r racers.
   Ḋ                    Remove first element of the list (its a garbage s value)
    z⁶                  Transpose with space as fill value.
      G                 Grid. Format the result.

您可以将第一个辅助链接替换为,‘X(与n递增配对,随机选择)。在主链接中,ṫ2可以用(出队)和变量代替
丹尼斯


2

TSQL,367个 345 341字节

打高尔夫球

DECLARE @r int=20, -- racers
        @g char=2  -- goal

DECLARE @ varchar(99)=REPLICATE('0',@r)a:PRINT @
DECLARE @A varchar(99)='',@s varchar(99)='',@i int=0WHILE @i<@r
SELECT
@i+=1,@A+=char(43-x*11),@s+=IIF(x=1,' ',LEFT(y,1)),@=RIGHT(@,@r-1)+IIF(x=1,' ',REPLACE(LEFT(@,1)+y,@g+1,' '))FROM(SELECT
IIF(LEFT(@,1)IN('',@g),1,0)x,ROUND(RAND(),0)y)z
PRINT @A+'
'+@s+'
'+REPLACE(@A,'+','=')IF @>''goto a

在线尝试

取消高尔夫:

DECLARE @r int=10, -- racers
        @g char=2  -- goal

DECLARE @ varchar(99)=REPLICATE('0',@r)
a:
PRINT @
DECLARE @A varchar(99)='',@s varchar(99)='',@i int=0

WHILE @i<@r
  SELECT
    @i+=1,
    @A+=char(43-x*11),
    @s+=IIF(x=1,' ',LEFT(y,1)),
    @=RIGHT(@,@r-1)+IIF(x=1,' ',REPLACE(LEFT(@,1)+y,@g+1,' '))
  FROM(SELECT IIF(LEFT(@,1)IN('',@g),1,0)x,ROUND(RAND(),0)y)z

PRINT @A+'
'+@s+'
'+REPLACE(@A,'+','=')

IF @>''GOTO a

请注意,测试站点上的随机种子将始终相同,每次给出的结果相同,在工作室管理中,它将给出不同的结果。可以为赛车手和目标使用不同的值来获得不同的图片


1

Python 3,237个字节

from random import*
def f(n,t):
 x='0'*n,;i=j=0;y=''
 while' '*n!=x[i]:
  if j==n:j=0;x+=y,;y='';print(x[i]);i+=1
  y+=' 'if x[i][j]in(' ',str(t))else eval(["'+'","str(randint(0,1))","'='","str(int(x[i-3][j])+int(x[i-1][j]))"][i%4]);j+=1

通过参数接受输入并输出到STDOUT的函数。这种方法利用了以下事实:对于所有赛车手,输出遵循周期四的周期,形式为“ +值=值”。通过使用模4的计数器,可以将包含每个步骤的期望值作为字符串的列表建立索引,并使用Python的eval函数评估结果。

怎么运行的

from random import*                       Import Python's random module to access the
                                          randint function
def f(n,t):                               Function with input number of racers n and target
                                          number t
x='0'*n,;i=j=0;y=''                       Initialise return tuple storage x, state number
                                          i, racer number j and append string y for x
while' '*n!=x[i]:                         Loop through all j for some i. If the current
                                          state consists only of spaces, all racers have
                                          finished, so stop
y+=...eval([...][i%4])...                 Index into list, using i mod 4, to find the
                                          desired process for the cycle step, and append to
                                          y
(If first step of cycle)
...+...                                   Plus sign
(If second step of cycle)
...str(randint(0,1))...                   Random number from (0,1)
(If third step of cycle)
...=...                                   Equals sign
(If fourth step of cycle)
...str(int(x[i-3][j])+int(x[i-1][j]))...  Addition of random number to previous racer
                                          'score'
...' 'if x[i][j]in(' ',str(t))...         But append space if the racer has previously
                                          finished, or has reached the target
...j+=1                                   Increment j
if j==n:j=0;x+=y,;y='';print(x[i]);i+=1   If j=n, all j must have been looped through.
                                          Reset j, append new state y to x, reset y, print
                                          current state to STDOUT and increment i. When
                                          this first executes, x contains only the initial
                                          state, meaning that this is printed and the cycle
                                          starts with the second state.

在Ideone上尝试


1

Python 2 191字节

from random import*
def c(p,w,r=[],l=0):
 while p:
	p-=1;s='0'
	while`w`>s[-1]:s+="+%s="%randint(0,1);s+=`eval(s[-4:-1])`;l+=2
	r+=[s]
 for z in map("".join,zip(*(t+l*' 'for t in r))):print z

在线尝试!


Python 3,200字节

from random import*
def c(p,w,r=[],l=0):
 while p:
  p-=1;s='0'
  while str(w)>s[-1]:s+="+%s"%randint(0,1);s+="=%s"%eval(s[-3:]);l+=2
  r+=[s]
 for z in map("".join,zip(*(t+l*' 'for t in r))):print(z)

在线尝试!


0

Python 2,278字节

import random
r=5
w=2
s=(0,)*r
while s.count(w)<len(s):
    print ''.join(map(lambda a:str(a),s))+"\n"+'+'*len(s)
    s = tuple(map(lambda x: x<w and x+random.randrange(2) or x,s))
    print ''.join(map(lambda a:str(a), s))+"\n"+'='*len(s)
    s = tuple([x for x in s if x!= w])

其中r是编号。赛车,w是获胜的分数

在这里尝试!


2
我测试了您的程序,它没有显示问题中描述的结果,所有内容都移到了左侧。
t-clausen.dk

0

Perl 5,150个字节

$,=$";say@n=(0)x(@e=('=')x(@p=('+')x<>));$t=<>;while(grep$_<$t,@n){@r=map{$_+=($g=0|rand 2);$g}@n;for$l(p,r,e,n){say map{$n[$_]>$t?$":$$l[$_]}0..$#n}}

在线尝试!

首先输入的是赛车手的数量,其次是需要的分数。

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.