给定下面列表中的ASCII运算符和一个数字n,使用该运算符作为字符,绘制出运算符的ASCII表示形式,该运算符的线段具有长度n。
一个来自列表= + - x /
的ASCII字符和一个整数n
( n >= 1
)。(我使用的是x
而不是*
和/
,而不是÷
,但您可以使用任何一种,无论哪一种更简单)。对于+
和x
,您只需处理奇数就可以避免对齐问题。
由长度为n的字符组成的运算符的ASCII绘图。水平部分应在字符之间留有空格,以抵消垂直/水平距离的差异。
这是密码-高尔夫,所以以字节为单位的最短代码将获胜。标准漏洞是被禁止的。
输入:+ 3
+
+ + +
+
输入:= 4
= = = =
= = = =
输入:= 10
= = = = = = = = = =
= = = = = = = = = =
输入:- 2
- -
输入:/ 10
/
/
/
/
/
/
/
/
/
/
输入:x 7
x x
x x
x x
x
x x
x x
x x
发布于 2016-11-07 17:36:48
(i,c)=>if(c<44){val b=(" "*(i-1)+"+\n")*((i-1)/2)
b+"+ "*i+"\n"+b}else
if(c<46)"- "*i else
if(c<48)i-1 to(0,-1)map(" "*_+"/\n")mkString else
if(c<62)"= "*i+"\n"+"= "*i
else{val a=0 to i/2-1 map(x=>" "*x+"x"+" "*((i/2-x)*2-1)+"x"+" "*x+"\n")mkString;a+" "*(i/2)+"x"+a.reverse}
用法:
val f:((Int,Char)=>String)=...
print(f(10, '/'))
解释:
代码测试char的ascii值,以选择生成映像的正确方式。所述运算符的ascii值为:('+' -> 43), ('-' ->45), ('/' -> 47), ('=' -> 61), ('x' -> 120)
。
(i,c)=> //define a function
if(c<44){ //if c is a plus
val b=(" "*(i-1)+"+\n")*((i-1)/2) //define the top/bottom part b as (i-1)/2 times (i-1) spaces, a plus sign and a newlineine
b+"+ "*i+"\n"+b //return b, i times a plus and a space, a newline and b
}else if(c<46) //if c is a '-'
"- "*i //return "- " repeated i times
else if(c<48) //if c is a '/'
i-1 to(0,-1) //create a range from i-1 to 0 in steps of -1
map(" "*_+"/\n") //map each number to that number of spaces plus a "/" and a newline
mkString //join them together
else if(c<62) //if c is '='
"= "*i+"\n"+"= "*i //return "= " repeated i times, a newline and "= " repeated i times again
else{ //if c if 'x'
val a= //define a, which will be the top part, as...
0 to i/2-1 //a range from 0 to i/2-1
map(n=> //map each number n to
" "*n //n spaces
+"x" //plus an "x"
+" "*((i/2-n)*2-1) //plus ((i/2)-n)*2-1 spaces
+"x" //plus an "x"
+" "*n //plus n spaces
+"\n" //and a newline
)mkString; //join them
a+" "*(i/2)+"x"+a.reverse //return a, i/2 spaces, "x" and the reverse of a
}
发布于 2016-11-08 11:12:33
(c,n)=>[...Array(n--)].map((_,i,a)=>a.map((_,j)=>({'/':a=i+j-n,x:a&&i-j,'-':a=i+i-n,'+':a&&j+j-n,'=':a+2&&a-2}[c]?' ':c)).join(c=='='|c<'/'?' ':'')).join`\n`
其中\n
表示文字换行符。
发布于 2016-11-08 14:50:15
e=#~Mod~2==1&;StringRiffle[Normal@SparseArray[{a_,b_}/;(a+b==d+1||a==b)[e@b&&a<3,,e@b&&(a==⌈d/2⌉||b==d),,e@b&&a<2,,a+b==d+1][[Last@ToCharacterCode@#~Mod~10]]:>#,{d=#2,2d-1}," "],"
"," "]&
匿名函数。接受字符串和数字作为输入,并返回字符串作为输出。到目前为止还不是最短的,但写起来还是很有趣的。
https://codegolf.stackexchange.com/questions/98890
复制相似问题