该挑战使用“ +”字符


28

您的任务:给定一个数字n,生成一个“ +”号,该符号n远离其中心。如果这令人困惑,请签出测试用例。

输入的标准方法:输出必须是字符串或打印的。有标准漏洞。

Input: 1
Output: +           ] 1 away from center `+`.

Input: 2
Output:  +          ] 2 away from center `+`.
        +++         ] 1 away from center `+`.  
         +

Input: 3
Output:   +         ] 3 away from center `+`.
          +
        +++++
          +
          +

这是,所以最短的代码获胜!


我假设允许在每行上使用尾随空格,以便结果为正方形,对吗?
路易斯·门多

@LuisMendo是的,很好。
“ SparklePony同志” 17年


3
“生成一个距其中心n个字符的'+'符号”-我不明白这部分。您指的是什么中心?事物怎么会怪异于自身?请澄清。
Wossname

6
如果中心距离自己的距离为0,那么混乱会少很多。
停止Haronica Monica

Answers:


45

木炭,5字节

P+×+N

在线尝试!


22
这是什么语言?
DJMcMayhem

@DJMcMayhem基本上,您可以使用它来执行乌龟图形ASCII艺术。它具有许多方便的内置函数,用于呈现不同类型的ASCII形状(如P+=十字)。
fergusq

这些不是多字节字符吗?
佩塔

3
@Petah木炭使用自定义代码页
ASCII码仅ASCII

@fergusq您可能正在将木炭与Turtlèd:P 混在一起,木炭并不是真正的乌龟图形语言
ASCII码,仅ASCII


12

JavaScript(ES6),67 65 63 60 59字节

x=>(v=(` `[r=`repeat`](--x)+`+
`)[r](x))+`+`[r](x*2)+`+
`+v;
  • 通过用替换两个出现的位置x-1(第一个为--x和第二个为)来节省2个字节x
  • 多亏了Kritixi Lithos节省了2个字节,替换"\n"`[newline]`
  • 多亏了user2428118,节省了3个字节,最终帮助我找到了一种以repeat减小大小的方式为别名的方法。(也对玛丽的努力表示敬意)
  • 多亏了Herman,间接节省了1个字节。

试试吧

f=
x=>(v=(` `[r=`repeat`](--x)+`+
`)[r](x))+`+`[r](x*2)+`+
`+v;
oninput=_=>o.innerText=f(+i.value)
o.innerText=f(i.value=3)
<input id=i min=1 type=number><pre id=o>


1
我不能完全肯定,但我认为你可以代替"\n"两个反引号和它们之间的面值换行符
Kritixi LITHOS

谢谢,@KritixiLithos; 不知道为什么我自己没想到这个。
毛茸茸的

1
它看起来像您可以通过别名重复,例如可能挽救一个字节a='repeat',v=.....` `[a]
玛丽

谢谢,@玛丽;我曾尝试过别名repeat()但最初的几次尝试又增加了2或3个字节,所以我放弃了它!当我回到电脑前时,我会再看看它。
毛茸茸的

2
x=>(v=(` `[r='repeat'](--x)+`+<newline>`)[r](x))+`+`[r](x*2+1)+`<newline>`+v
user2428118

9

MATL,11个字节

tZv=&+g43*c

在线尝试!

举例说明

考虑 n = 3

t     % Implicitly input n. Duplicate
      % STACK: 3, 3
Zv    % Symmetric range
      % STACK: 3, [1 2 3 2 1]
=     % Equal, element-wise
      % STACK: [0 0 1 0 0]
&+    % All pair-wise additions. Gives a 2D array
      % STACK: [0 0 1 0 0;
                0 0 1 0 0;
                1 1 2 1 1;
                0 0 1 0 0;
                0 0 1 0 0]
g     % Logical: convert non-zero to one
      % STACK: [0 0 1 0 0;
                0 0 1 0 0;
                1 1 1 1 1;
                0 0 1 0 0;
                0 0 1 0 0]
43*   % Multiply by 43 (ASCII for '+'), element-wise
      % STACK: [ 0  0 43  0  0;
                 0  0 43  0  0;
                43 43 43 43 43;
                 0  0 43  0  0;
                 0  0 43  0  0]
c     % Convert to char. Char 0 is displayed as space. Implicitly display.
      % STACK: ['  +  ';
                '  +  ';
                '+++++';
                '  +  ';
                '  +  ']

8

木炭 16 13字节

Nα×+α←↑×+α‖O↘

在线尝试!

使用与其他木炭答案不同的方法。

说明

Nα                       # Take input and store it in variable α
×+α                       # α times write a +
←                         # Go left
↑×+α                      # α times write a + upwards

现在左上角已经完成,看起来像这样:

  +
  +
+++

‖O↘                      # Reflect-overlap it in a SE direction

最后一步是此程序的关键,它使用加号的左上部分通过向东南方向(向右和向下)反映加号来生成其余的加号。


木炭有自己的代码页吗?这些字符中有许多是UTF-8中的多个字节。
TRiG

@TRiG是的,确实如此
Kritixi Lithos

8

莎士比亚编程语言749 743字节

N.Puck,.Page,.Ford,.Ajax,.Act I:.Scene I:.[Enter Puck and Ford]Puck:Listen to thy heart!Ford:You is the difference between a cat and I.Scene V:.[Exeunt][Enter Page and Ajax]Ajax:You is the difference between a cat and Ford.Scene X:.Page:You is the product of Puck and I.Is you as big as zero?If so,you is the sum of the sum of the sum of a big big big big big cat and a big big big cat and a big cat and a cat.If not,you big big big big big cat.Speak thy mind!Ajax:You is the sum of you and a cat.Is you as big as Ford?If not,let us return to Scene X.Page:You is the sum of a big big big cat and a big cat.Speak thy mind![Exit Page][Enter Puck]Ajax:You is the sum of you and a cat.Is you as big as Ford?If not,let us return to Scene V.[Exeunt]

在线尝试!

编辑:使答案与官方SPL实施兼容-我之前无法使其正常工作。

占用6个字节,因为场景编号不必连续。

讲解

SPL是一种设计得像莎士比亚戏剧的世界一流作品。正名词的值为1(在这里使用cat),负名词的值为-1(没有使用,但是pig是其中之一)。形容词通过将常数乘以2来修饰常数。

N.

直到第一个点之前的所有内容都是标题,并不重要。

Puck,.                           row counter
Page,.                           column counter
Ford,.                           input
Ajax,.                           temp

字符是整数变量,它们每个都有一个堆栈,但是我不需要使用该功能。

Act I:.
Scene I:.

行为和场景用作转到标签

[Enter Puck and Ford]

仅在舞台上恰好有两个角色同时使用时才有用。

Puck:Listen to thy heart!

读取一个数字并使福特记住它。

Ford:You is the difference between a cat and I.

如您所见,Engrish在SPL中有效。这使Puck的价值“和我的猫不同”。但是这是什么意思?cat是一个正名词,所以是Puck = 1 - Ford

Scene II:.
[Exeunt]

Exeunt只是“ exit”的复数形式,没有参数意味着舞台上的每个人都退出。

[Enter Page and Ajax]
Ajax:You is the difference between a cat and Ford.

这也是,Page = 1 - Ford但由另一位演员讲,所以I是错误的。由于这是一个循环,因此我不能只复制的值Puck

Scene III:.
Page:You is the product of Puck and I.

现在非常简单。Ajax = Puck * Page

Is you as big as zero?

“ as [adj] as”是==运算符。

If so,you is the sum of the sum of the sum of a big big big big big cat and a big big big cat and a big cat and a cat.

如果Ajax == 0 ...“ cat”为1,“ big cat”为2,“ big big cat”为4,依此类推。替换简单常数后,我们得到“ 32和8与2和1之和的总和”->“ 40与2和1与之和的总和”->“ 42和1与之和” ->“ 43”,是+的ASCII码。

If not,you fat fat fat fat fat cat.

否则,它只是“胖胖胖胖猫”,因此Ajax会获得32的值,即一个空格的ASCII码。

Speak thy mind!

这是输出字符的命令。

Ajax:
You sum you and cat.Is you as big as Ford?If not,let us return to Scene III.

这是一个循环构造。“您为您和猫总和”递增Page,并且if(Page != Ford) goto Scene III。该程序的其余部分使用相同的组件,因此这是一个更具可读性的伪代码版本:

场景1:
    输入= [输入编号];
    行= 0-输入+ 1;
场景2:
    col = 0-输入+ 1;
场景3:
    temp =行* col;
    if(temp == 0){
        temp ='+';
    }其他{
        temp ='';
    }

    putchar(temp);
    页=页+ 1;
    if(Page!= Ford)转到Scene3;
    阿贾克斯= 10;
    putchar(Ajax);
    冰球=冰球+1;
    if(Puck!= Ford)转到Scene2;

If not,let us return to Scene III.” -1; 打破第四堵墙:P
雅各布


6

Mathematica,39个字节

Print@@@(CrossMatrix[#-1]"+"/. 0->" ")&

CrossMatrix是一个内置函数,它使用1s而不是+s和0s而不是空格来生成所需形状的矩阵。如果我们将该矩阵乘以,"+"1+s 替换s,同时保持0s不变(显然... 0*x = 01*x = x,对吗?)。然后,使用手动将零替换为空格/. 0->" "。最后,我们使用来打印矩阵的每一行Print@@@(...)


1
不知道Print可以那样使用。
ngenisis

6

C,69字节

不太有趣...在正方形上循环,打印出适当的字符。

r,c;f(n){for(r=-n;++r<n;puts(""))for(c=-n;++c<n;putchar(r*c?32:43));}


6

GNU sed104 99字节

-5感谢seshoumara
包括+1-r

s/1//;h;:;s/(.*)1/ \12/;t;s/( *2)2(2*)/\1\n\1\2/
t;G;s/1+/&&1/;s/(.*)(\n1*)/&\n\1/;/1/!c+
y/12/++/

一元输入

在线尝试!

s/1//                    # Subtract 1 from input
h                        # Hold onto input
:                        # Start loop
s/(.*)1/ \12/            #   Remove a 1, prepend a space, and append a 2
t                        # Loop until all 1s are 2s
                         # Start Loop (uses the previous label)
s/( *2)2(2*)/\1\n\1\2/   #   Shift all but the first 2 from the last line to a new line 
                         #   E.g.  "  2"      "  2"
                         #         "  222" -> "  2"
                         #                    "  22"
t                        # Loop until all 2s are on their own line
G                        # Append a newline and input
s/1+/&&1/                # Double the number of 1s and append an extra
s/(.*)(\n1*)/&\n\1/      # Copy all of the lines with 2s to the end
/1/!c+                   # If there aren't any 1s print a '+'
y/12/++/                 # Convert all 1s and 2s to +s

1可以通过使用保存5个字节s/( *2)2(2*)/\1\n\1\2/s/(.*)(\n1*)/&\n\1/如图这里,为99的总成绩
seshoumara

5

LUA 113,90个字节

r,w,p=string.rep,io.read(),io.write;s=r(' ',w-1)p(r(s..'+'..'\n',w-1))p(r('+',w*2-1)..'\n')p(r(s..'+'..'\n',w-1))

r,w=string.rep,io.read()d=w*2-1;for a=1,d do print(a~=w and r(' ',w-1)..'+'or r('+',d))end

5

Python 2,52字节

n=input()-1
p=(' '*n+'+\n')*n
print p+'++'*n+'+\n'+p

在线尝试!

53字节替代(TIO):

n=input()-1
for c in' '*n+'+'+' '*n:print c*n+'+'+c*n

5

R,54个字节

感谢@Jarko Dubbeldam,减少了7个字节:

function(n){a=matrix("",y<-n*2-1,y);a[n,]=a[,n]="x";a}

先前的答案:

f=function(n){a=matrix("",n*2-1,n*2-1);a[n,]="x";a[,n]="x";a}

1
您不必命名函数,因此function(n){a=matrix("",n*2-1,n*2-1);a[n,]="x";a[,n]="x";a}为59个字节!
JAD

1
另外,您可以使用matrix("",y<-n*2-1,y)
JAD

1
a[n,]=a[,n]="x"也可以工作,节省更多字节。
JAD

您可以通过使用另一个scan()程序并使之成为一个函数而不是一个函数来保存另外4个字节:n=scan();a=matrix("",y<-n*2-1,y);a[n,]=a[,n]="+";a
rturnbull

4

PowerShell,48字节

param($n)($x=,(" "*--$n+"+")*$n);'+'*(1+2*$n);$x

在线尝试!

接受输入$n。从构造一个以串联的--$n空格开始+。使用逗号运算符(新减少的)将其转换为数组$n。该数组存储在$xparen 中并封装在paren中,以在管道上放置副本。

然后,我们执行中间部分,该部分将+字符串乘以适当的次数。那留在管道上。最后,我们$x再次上线。

这些都在程序完成时留在管道上,隐式Write-Output在元素之间插入换行符。


4

Perl 5,45个字节

44个字节的代码+ -p标志。

$_=join"+
",@%=($"x--$_)x$_,"+"x($_*2),@%,""

在线尝试!


一些类似(但仍然不同)的方法:

48个字节(47+ -p):

$_=join"+"x($_*2-1).$/,(~~($"x--$_."+\n")x$_)x2

50个字节(超过49个字节-n):

$,="+"x($_*2-1).$/;print+(~~($"x--$_."+\n")x$_)x2


3

CJam,23个字节

ri_(S*'++a\2*(*_z..e>N*

在线尝试!

说明

感觉有些欠佳,但想法是将以下两个网格叠加:

  +
  +
  +
  +
  +



+++++

这给出了预期的结果。

ri    e# Read input and convert to integer N.
_(    e# Duplicate and decrement.
S*    e# Get a string of N-1 spaces (indentation of the vertical bar).
'++   e# Append a + (the vertical bar).
a     e# Wrap the line in an array.
\2*(  e# Swap with the other copy of N and compute 2N-1.
*     e# Repeat the line that many times.
_z    e# Duplicate the grid and transpose it.
..e>  e# Pairwise maximum between the two grids. This superimposes them.
N*    e# Join with linefeeds.

3

CJam,17岁

ri(S*_]'+*_ffe>N*

在线尝试

说明:

ri(      read n, convert to int and decrement
S*       make a string of n-1 spaces
_]       duplicate it and put the 2 strings in an array
'+*_     join the strings with a '+' and duplicate the result
ffe>     for each pair of characters from the 2 (identical) strings,
          get the larger character (results in a matrix)
N*       join the rows with newlines




2

JS(ES6),88 74 73字节

x=>(x--,y=y=>(" ".repeat(x)+`+
`).repeat(x),y``+"+".repeat(x+x+1)+"\n"+y``)

大概可以打更多的高尔夫球。

Snippetify(x=>(x--,y=y=>(" ".repeat(x)+`+
`).repeat(x),y``+"+".repeat(x+x+1)+"\n"+y``))
<script src="https://programmer5000.com/snippetify.min.js"></script>
<input type = "number">
<pre data-output></pre>


1
我不能完全肯定,但我认为你可以代替"\n"两个反引号和它们之间的面值换行符
Kritixi LITHOS

只看到现在的解决方案,您就把我击败了几分钟。在很短的时间内发布的相同语言的类似解决方案的礼节如何?
毛茸茸的

2

JavaScript(ES6),60个字节

f=
n=>(r=([s,t])=>(s=s.repeat(n-1))+t+s+`
`)([r(` +`),r(`++`)])
<input type=number min=1 oninput=o.textContent=f(this.value)><pre id=o>

输出两个尾随换行符。替代公式,也为60个字节:

n=>(r=a=>(s=a[0].repeat(n-1))+a[1]+s+`
`)([r(` +`),r(`++`)])

2

PowerShell,48岁

似乎没有比这更短的方法了(和其他解决方案几乎一样):

($a=,(' '*($n="$args"-1)+'+')*$n)
'+'+'++'*$n
$a

要么

($a=(' '*($n="$args"-1)+'+
')*$n)+'++'*$n+"+
$a"


2

REXX,81个字节

arg a
b=a*2-1
do i=1 to b
  if i=a then say copies('+',b)
  else say right('+',a)
  end

2

PHP, 68 Bytes

for(;$i<$c=-1+2*$m=$argn;)echo"\n".str_pad("+",$c," +"[$m==++$i],2);

83 Bytes

for(;$i<($c=($n=$argn)*2-1)**2;)echo$i%$c?"":"\n".!++$k," +"[$k==$n|$i++%$c==$n-1];

1
You can save a few bytes by using $m=$argn and pre-incrementing $i rather than post incrementing it. you can also save a byte by moving the $m assignment ot the end and dropping the brackets.
user59178

@user59178 I could not understand what you exactly mean
Jörg Hülsermann

1
for(;$i<$c=-1+2*$m=$argn;)echo"\n".str_pad("+",$c," +"[$m==++$i],2);
user59178

while(++$y<2*$n=$argn)echo"\n",str_pad("+",$n*2-1," +"[$y==$n],2); 66 bytes (and save one more with a physical linebreak)
Titus


2

Brain-Flak, 216 + 1 = 217 bytes

+1 bytes from the -A flag

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

Try it online!

Explanation to come

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.