制作锯齿形


25

您的任务是接受一个整数输入,并使用斜杠和反斜杠打印锯齿形图案。

  • 整数输入确定每个曲折和曲折的长度,以及曲折和曲折的数量
  • 模式始终从右到左开始

测试用例

4->
   /
  /
 /
/
\
 \
  \
   \
   /
  /
 /
/
\
 \
  \
   \
2->
 /
/
\
 \
0->
1->
/
8->
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \
       /
      /
     /
    /
   /
  /
 /
/
\
 \
  \
   \
    \
     \
      \
       \

3
我们可以为每行输出一个数组/字符串列表吗?是否允许培训或引导换行符或空格?
毛茸茸的

2
只要模式不受影响,引导空格就可以吗?
Emigna

Answers:


10

C(gcc)108 102 101 98 80 76 72字节

  • 感谢Kevin Cruijssen保存了六个字节;去除括号和高尔夫球场N-n-1N+~n
  • 通过将Z的增量移入循环条件来保存一个字节
  • 使用printf("%c\n",...)代替putchar(...)和节省了三个字节,puts("")
  • 由于HatsuPointerKun,节省了十八(!)个字节;使用printf("%*s",n,"");打印n的空间,而不是使用一个循环j;for(j=n;j--;)putchar(32);,并结合双方printf(...);通话
  • 使用printf("%*c",-~n,...);代替节省了四个字节printf("%*s%c",n,"",...);
  • 由于nwellnhof节省了四个字节;将所有内容移动到一个循环而不是两个循环内
j;f(k){for(j=0;j<k*k;j++)printf("%*c\n",j/k%2?j%k+1:k-j%k,j/k%2?92:47);}

在线尝试!


Z,n,j;f(N){for(Z=0;Z<N;Z++)for(n=N;n--;putchar(Z%2?92:47),puts(""))for(j=Z%2?N+~n:n;j--;)putchar(32);} 102个字节。通过将所有内容放入循环中来删除花括号;并更改N-n-1N+~n
凯文·克鲁伊森

1
@KevinCruijssen谢谢。通过交换Z%2?...:...并替换Z<N;Z++为保存了另一个字节Z++<N;
乔纳森·弗雷希

1
您可以像我在回答中那样使用printf魔术来节省几个字节。这样,您将摆脱用于打印空间的for循环。有关更多详细信息,请参见有关带有printf的左侧填充空间的堆栈溢出答案
HatsuPointerKun

@HatsuPointerKun谢谢;这是重复空格C.一个非常短的路
乔纳森富来

短4个字节:i;f(N){for(i=0;i<N*N;i++)printf("%*c\n",i/N%2?i%N+1:N-i%N,i/N%2?92:47);}在线尝试!
nwellnhof


4

MATL,17个字节

:"GXy@o?P47}92]*c

在线尝试!

说明

:         % Implicit input, n. Push range [1 2 ... n]
"         % For each k in that range
  G       %   Push n again
  Xy      %   Identity matrix of that size
  @       %   Push k
  o?      %   If it's odd
    P     %     Flip the matrix upside down
    47    %     Push 47 (ASCII for '/')
  }       %   Else
    92    %     Push 92 (ASCII for '\')
  ]       %   End
  *       %   Multiply each entry of the matrix by that number
  c       %   Convert to char. Char 0 is shown as space
          % Implicit end. Implicit display

4

C#(.NET核心)117个 103 101字节

a=>{for(int z=a+1,e=0;e<a*a;)System.Console.WriteLine(e++/a%2<1?"/".PadLeft(--z):@"\".PadLeft(z++));}

在线尝试!


您可以保存14个字节是这样的:a=>{var o="";for(int z=a+1,e=0;e<a*a;)o+=(e++/a%2<1?"/".PadLeft(--z):@"\".PadLeft(z++))+"\n";return o;} 103字节你并不需要所有这些括号; 你可以结合int; 并且只添加+"\n"一次。
凯文·克鲁伊森



3

SOGL V0.1213 12 9 个字节

╝F{±↔}P}ø

在这里尝试!

╝F{±↔}P}如果不需要0测试用例,则可以为8个字节

说明:

       }   implicitly started loop repeated input times
╝            create a down-right diagonal of the input
 F           get the current looping index, 1-indexed
  {  }       that many times
   ±↔          reverse the diagonal horizontally
      P      print that
        ø  push an empty string - something to implicitly print if the loop wasn't executed


3

Mathematica,84岁 90 个字节

(n=#;Grid@Array[If[Abs[n-(s=Mod[#-1,2n])-.5]==#2-.5,If[s<n,"‌​/","\\"],""]&,{n^2,n‌​}])&
  • 感谢Jenny_mathy -6个字节。

我不知道为什么\明显比暗/

enter image description here


2
84个字节(n=#;Grid@Array[If[Abs[n-(s=Mod[#-1,2n])-.5]==#2-.5,If[s<n,"/","\\"],""]&,{n^2,n}])&
J42161217 '17

3

JQ 1.594 89个字节

["/","\\"][range($n)%2]as$s|range($n)|[(range(if$s=="/"then$n-.-1 else. end)|" "),$s]|add

说明

  ["/","\\"][range($n)%2] as $s                         # for $s= / \ / \ $n times 
| range($n)                                             # for .=0 to $n-1
| [(range(if $s=="/" then $n-.-1 else . end)|" "), $s]  # form list of spaces ending with $s
| add                                                   # concatenate

样品运行

$ jq -Mnr --argjson n 5 '["/","\\"][range($n)%2]as$s|range($n)|[(range(if$s=="/"then$n-.-1 else. end)|" "),$s]|add'
    /
   /
  /
 /
/
\
 \
  \
   \
    \
    /
   /
  /
 /
/
\
 \
  \
   \
    \
    /
   /
  /
 /
/

在线尝试!


3

爪哇8,140个 134 116字节

n->{String r="";for(int a=0,b,c;a++<n;)for(b=n;b-->0;r+=a%2>0?"/\n":"\\\n")for(c=b-n+b|-a%2;++c<b;r+=" ");return r;}

-24个字节感谢@Nevay

说明:

在这里尝试。

n->{                // Method with integer parameter and String return-type
  String r="";      //  Result-String
  for(int a=0,b,c;  //  Index integers
      a++<n;)       //  Loop (1) from 0 to the input (exclusive)
    for(b=n;        //   Reset `b` to the input
        b-->0;      //   Inner loop (2) from the input to 0 (exclusive)
                    //     After every iteration: 
        r+=a%2>0?"/\n":"\\\n") 
                    //      Append either of the slashes + a new-line
      for(c=b-n+b|-a%2;++c<b;r+=" ");
                    //    Append the correct amount of spaces
                    //   End of inner loop (2) (implicit / single-line body)
                    //  End of loop (1) (implicit / single-line body)
  return r;         //  Return the result-String
}                   // End of method

1
最内层循环的条件可以写为c-->f*(b-n-~b)(-6字节)。
Nevay

1
116字节:n->{String r="";for(int a=0,b,c;a++<n;)for(b=n;b-->0;r+=a%2>0?"/\n":"\\\n")for(c=b-n+b|-a%2;++c<b;r+=" ");return r;}
Nevay

3

的Javascript ES8,83 79 78 76 75 74 71字节

*感谢Shaggy,ES8减少了1个字节

A=(m,i=0)=>i<m*m?`/\\`[x=i/m&1].padStart(x?i%m+1:m-i%m)+`
`+A(m,++i):""

在这里测试


谁反对我的解决方案,您能否解释原因?我想念什么吗?
DanielIndie

2
我不是投票否决的人,但我认为这是因为功能需要可重复才能有效。通过i设置默认参数可以很容易地修复您的问题。字节计数似乎也没有。
Emigna

1
添加TIO链接始终受到人们的欢迎,这样人们就可以轻松测试您的解决方案。
Emigna

1
@Emigna修复了它(char明智和Link明智):)
DanielIndie

1
74字节,带有一些ES8。另外,对于JS,您可以仅使用堆栈代码段,而不要使用TIO。
毛茸茸的


2

PowerShell,81个字节

param($a)if($a){1..$a|%{((1..$a|%{" "*--$_+'\'}),($a..1|%{" "*--$_+'/'}))[$_%2]}}

在线尝试!

gh,这很丑。如此多的重复代码,再加上7个字节来说明0特殊情况。欢迎打高尔夫球。


2

Pyth,17个字节

js<*_+RV"\/"_B*L;

在线尝试:演示

说明:

js<*_+RV"\/"_B*L;QQQ   implicit Qs at the end
              *L;Q     list with ["", " ", "  ", ..., " "*(input-1)]
            _B         bifurcate with reverse: [["" to "   "], ["   " to ""]]
     +RV"\/"           append to each one either "\" or "/": 
                       [["\", to "   \"], ["   /" to "/"]]
    _                  reverse
   *              Q    repeat input times
  <                Q   but only take the first input many
 s                     flatten the list of lists
j                      print on each line

2

Python 3:90字节 82字节

lambda n:"\n".join(" "*(abs(i%(n*2)-n+i//n%2)-1)+"/\\"[i//n%2]for i in range(n*n))

感谢@Jonathan Frech指出不需要打印,并且第一个Zig是错误的方式


] for-> ]for
乔纳森·弗雷希

您不需要print(...),返回字符串的函数将是有效的。另外,我认为您的初始Zig方向错误(\而不是/)。
乔纳森·弗雷希

@JonathanFrech谢谢!我改变了它
Bassintag

1
(abs(...)-1)-> ~-abs(...)
乔纳森·弗雷希

2

05AB1E17 16字节

F<„/\Nèú.sNƒR}»,

在线尝试!

说明

F                  # for N in [0 ... input-1] do
  „/\              # push the string "/\"
     Nè            # cyclically index into this string with N
 <     ú           # prepend input-1 spaces to this string
        .s         # get suffixes
          NƒR}     # reverse the list of suffixes input+1 times
              »,   # join on newline and print

当前使用画布的最佳尝试:

F„/\Nè©53NèΛ2®ð«4Λ



2

Dyalog APL39 36 35 34字节

{↑((,⍵ ⍵⍴(⌽,⊢)⍳⍵)/¨' '),¨⍵/⍵⍴'/\'}

在线尝试!

Zacharý节省了1个字节


撞死了,打败我一个字节。您可以使⎕IO成为0,然后删除¯1+
扎卡里

@Zacharý我正要这样做:p
dzaima

哦,还有一两件事:(⌽,⊢)⍳⍵不是(⌽⍳⍵),⍳⍵
扎卡里

@Zacharý是的,我还不了解随之而来的大头钉,默契和其他内容:/
dzaima

不用担心,我也不完全理解火车/叉车/无论怎样的工作方式。
扎卡里



1

Excel VBA,84 83字节

匿名VBE立即窗口功能,可接收范围内的输入[A1]并输出到VBE立即窗口

For i=1To[A1]:For j=1To[A1]:?IIf(i mod 2,Space([A1]-j)&"/",Space(j-1)&"\"):Next j,i


0

Haskell86 85字节

f n=take(n*n)$cycle$[(' '<$[x..n-1])++"/"|x<-[1..n]]++[(' '<$[2..x])++"\\"|x<-[1..n]]

在线尝试!

感谢Laikoni,节省了一个字节

重复曲折++曲折,并采取第一n*n行。


cycle$ ...而不是cycle( ... )保存一个字节。
Laikoni

@Laikoni谢谢!
jferard



0

D,105字节

import std.stdio;void m(T)(T n){for(T i,j;i<n;++i)for(j=0;j<n;++j)printf("%*c\n",i%2?j+1:n-j,i%2?92:47);}

在线尝试!

摘自HatsuPointerKun的C ++答案。

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.