การแข่งขันจำนวนลง


10

งานของคุณคือการสร้างโปรแกรมที่เพิ่มตัวเลขสุ่มไปยังผลรวมก่อนหน้าในการเปิดไพ่แข่งจำนวนมาก

นักแข่งแต่ละคน (คอลัมน์) เริ่มต้นที่ 0 และเพิ่ม 1 หรือ 0 เข้ากับผลรวมก่อนหน้าในแต่ละขั้นตอนของการแข่งขันจนกว่านักแข่งทุกคนจะถึงคะแนนที่จำเป็นในการชนะ ควรเลือก 1 หรือ 0 โดยการสุ่ม (นิยามมาตรฐานของการสุ่มสามารถดูได้ที่นี่ ) ผลลัพธ์จะแสดงผลลัพธ์ของการแข่งขันโดยแต่ละคอลัมน์แสดงถึงนักแข่งหนึ่งคนในรูปแบบนี้:

>> 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. คะแนนที่ต้องชนะซึ่งจะต้องมากกว่าหนึ่งคะแนน

นี่คือโค้ดกอล์ฟซึ่งโปรแกรมที่มีจำนวนไบต์น้อยที่สุดจะเป็นผู้ชนะ

แก้ไข: มีคะแนนสูงสุดที่ไม่สามารถบังคับใช้ได้ 9 คะแนน - นี่คือการรักษาความสมบูรณ์ของคอลัมน์ นอกจากนี้ยังสามารถเว้นช่องว่างระหว่างคอลัมน์ในเอาต์พุต


จำนวนสูงสุดของคอลัมน์และคะแนนสูงสุดที่ต้องได้รับการสนับสนุนคืออะไร?
nanofarad

ไม่มีค่าสูงสุดที่กำหนดดังนั้นจึงเป็นเช่นเดียวกับค่าต่ำสุด: อย่างน้อยสามคอลัมน์และคะแนนสองรายการ
กายวิภาคศาสตร์

3
คะแนนที่ต้องการจะมีตัวเลขสองหลักหรือไม่
Leun Nun

4
"ต้องใส่เฉพาะตัวเลข + และ = ในเอาต์พุต" แล้วช่องว่างล่ะ
Leun Nun

ช่องว่างไม่จำเป็นต้องได้รับการเก็บรักษาไว้พวกเขาอยู่ในตัวอย่างเพื่อความชัดเจน คำถามที่ดีเกี่ยวกับตัวเลขสองหลัก - ฉันคิดว่ามีคะแนนสูงสุดซึ่งก็คือเก้า ฉันจะแก้ไขคำถาม
กายวิภาคศาสตร์

คำตอบ:


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สามารถถูกแทนที่ด้วย(dequeue) และกับตัวแปร
Dennis


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

ลองออนไลน์

Ungolfed:

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 วิธีการนี้ใช้ประโยชน์จากความจริงที่ว่าผลลัพธ์เป็นไปตามวัฏจักรของช่วงเวลาสี่ของรูปแบบ '+ value = value' สำหรับนักแข่งทั้งหมด ด้วยการใช้เคาน์เตอร์โมดูโลสี่รายการที่มีค่าที่ต้องการสำหรับแต่ละขั้นตอนเป็นสตริงสามารถทำดัชนีได้และผลลัพธ์ที่ประเมินโดยใช้ฟังก์ชัน Eval ของ Python

มันทำงานอย่างไร

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}}

ลองออนไลน์!

อินพุตแรกคือจำนวนนักแข่งและอันดับที่สองคือคะแนนที่จำเป็น

โดยการใช้ไซต์ของเรา หมายความว่าคุณได้อ่านและทำความเข้าใจนโยบายคุกกี้และนโยบายความเป็นส่วนตัวของเราแล้ว
Licensed under cc by-sa 3.0 with attribution required.