在框中绘制ASCII框


23

问题

给定输入 a,b,c

a,b,c正整数在哪里

a > b > c

用尺寸允许的任何字符组成一个盒子 a x a

将具有不同允许字符的框居中,且尺寸b x b在上一个框内

将另一个不同允许字符的框居中,其尺寸c x c在上一个框内

允许的字符为ASCII字符 [a-zA-z0-9!@#$%^&*()+,./<>?:";=_-+]

输入项 a=6, b=4, c=2

######
#****#
#*@@*#
#*@@*#
#****#
######

输入项 a=8, b=6, c=2

########
#******#
#******#
#**@@**#
#**@@**#
#******#
#******#
########

输入项 a=12, b=6, c=2

############
############
############
###******###
###******###
###**@@**###
###**@@**###
###******###
###******###
############
############
############

规则

  • 最短代码胜出
  • 请记住,您可以选择在给定范围内打印哪个字符
  • 尾随换行符被接受
  • 接受尾随空格
  • 函数可能会返回带有换行符的字符串,字符串数组或将其打印出来

5
输入是否总是有效的(即每个数字至少比前一个数字小2)?为了确保对称绘制,数字将始终是(全部为偶数)还是(全部为奇数)?
散布

非常类似于显示年轮的年龄
manatwork

1
@Christian前三行定义了这些要求,请让我知道它们是否足够。
LiefdeWen

@StefanDelport是的,我很想念。谢谢。
散布

Answers:



7

果冻 20  19 字节

-1字节使用快速`避免链接,正如Outgolfer的Erik建议的那样。

H»þ`€Ḣ>ЀHSUṚm€0m0Y

实际上,一个完整的程序[a,b,c]使用a:2 b:1 c:0
...来打印列表,实际上,它最多可以使用10个盒子,其中最里面的盒子是0例如)。

在线尝试!

怎么样?

H»þ`€Ḣ>ЀHSUṚm€0m0Y - Main link: list of boxes, B = [a, b, c]
H                   - halve B = [a/2, b/2, c/2]
    €               - for €ach:
   `                -   repeat left argument as the right argument of the dyadic operation:
  þ                 -     outer product with the dyadic operation:
 »                  -       maximum
                    - ... Note: implicit range building causes this to yield
                    -       [[max(1,1),max(1,2),...,max(1,n)],
                    -        [max(2,1),max(2,2),...,max(2,n)],
                    -        ...
                    -        [max(n,1),max(n,2),...,max(n,n)]]
                    -       for n in [a/2,b/2,c/2]
     Ḣ              - head (we only really want n=a/2 - an enumeration of a quadrant)
         H          - halve B = [a/2, b/2, c/2]
       Ѐ           - map across right with dyadic operation:
      >             -   is greater than?
                    - ...this yields three copies of the lower-right quadrant
                    -    with 0 if the location is within each box and 1 if not
          S         - sum ...yielding one with 0 for the innermost box, 1 for the next, ...
           U        - upend (reverse each) ...making it the lower-left
            Ṛ       - reverse ...making it the upper-right
             m€0    - reflect €ach row (mod-index, m, with right argument 0 reflects)
                m0  - reflect the rows ...now we have the whole thing with integers
                  Y - join with newlines ...making a mixed list of integers and characters
                    - implicit print - the representation of a mixed list is "smashed"

7

Python 2中,107个 103字节

a,b,c=input()
r=range(1-a,a,2)
for y in r:
 s=''
 for x in r:m=max(x,y,-x,-y);s+=`(m>c)+(m>b)`
 print s

全部程序,打印盒用a=2b=1c=0

较差的答案,具有列表理解(104字节):

a,b,c=input()
r=range(1-a,a,2)
for y in r:print''.join(`(m>c)+(m>b)`for x in r for m in[max(x,y,-x,-y)])

5

C#,274232字节

using System.Linq;(a,b,c)=>{var r=new string[a].Select(l=>new string('#',a)).ToArray();for(int i=0,j,m=(a-b)/2,n=(a-c)/2;i<b;++i)for(j=0;j<b;)r[i+m]=r[i+m].Remove(j+m,1).Insert(j+++m,i+m>=n&i+m<n+c&j+m>n&j+m<=n+c?"@":"*");return r;}

即使对于C#来说也很糟糕,所以绝对可以打高尔夫球,但是我的脑子变得一片空白。

完整/格式化版本:

using System;
using System.Linq;

class P
{
    static void Main()
    {
        Func<int, int, int, string[]> f = (a,b,c) =>
        {
            var r = new string[a].Select(l => new string('#', a)).ToArray();

            for (int i = 0, j, m = (a - b) / 2, n = (a - c) / 2; i < b; ++i)
                for (j = 0; j < b;)
                    r[i + m] = r[i + m].Remove(j + m, 1).Insert(j++ + m,
                        i + m >= n & i + m < n + c &
                        j + m > n & j + m <= n + c ? "@" : "*");

            return r;
        };

        Console.WriteLine(string.Join("\n", f(6,4,2)) + "\n");
        Console.WriteLine(string.Join("\n", f(8,6,2)) + "\n");
        Console.WriteLine(string.Join("\n", f(12,6,2)) + "\n");

        Console.ReadLine();
    }
}

您似乎已经恢复了头脑,j + m <= n + c可以成为 n + c > j + m
LiefdeWen

以及随后的i + m >= nn < i + m
LiefdeWen

您使用了i+m4次,因此您可以将其添加到变量中for以节省一些
LiefdeWen

没有正确检查这些内容,但是:您永远不会i孤立地使用它们,只是进行初始化i=m和比较i<b+m;或...只需使用i,init i=0但在on上循环i<a,然后在r[i]=new string('#',a),旁边添加j=0,并添加一个条件以检查i是否在j的循环范围内(这应该得到回报,因为您丢失了所有Linq)。
VisualMelon

3

Haskell,126个字节

f a b c=r[r["#*@"!!(v c+v b)|x<-[1..d a],let v k|x>a#k&&y>a#k=1|2>1=0]|y<-[1..d a]]where r x=x++reverse x;d=(`div`2);x#y=d$x-y

在线尝试!


3

的JavaScript(ES6),174个 170 147字节

a=>b=>c=>(d=("#"[r="repeat"](a)+`
`)[r](f=a/2-b/2))+(e=((g="#"[r](f))+"*"[r](b)+g+`
`)[r](h=b/2-c/2))+(g+(i="*"[r](h))+"@"[r](c)+i+g+`
`)[r](c)+e+d

试试吧

fn=
a=>b=>c=>(d=("#"[r="repeat"](a)+`
`)[r](f=a/2-b/2))+(e=((g="#"[r](f))+"*"[r](b)+g+`
`)[r](h=b/2-c/2))+(g+(i="*"[r](h))+"@"[r](c)+i+g+`
`)[r](c)+e+d
oninput=_=>+x.value>+y.value&&+y.value>+z.value&&(o.innerText=fn(+x.value)(+y.value)(+z.value))
o.innerText=fn(x.value=12)(y.value=6)(z.value=2)
label,input{font-family:sans-serif;}
input{margin:0 5px 0 0;width:50px;}
<label for=x>a: </label><input id=x min=6 type=number step=2><label for=y>b: </label><input id=y min=4 type=number step=2><label for=z>c: </label><input id=z min=2 type=number step=2><pre id=o>


说明

a=>b=>c=>            :Anonymous function taking the 3 integers as input via parameters a, b & c
(d=...)              :Assign to variable d...
("#"[r="repeat"](a)  :  # repeated a times, with the repeat method aliased to variable r in the process.
+`\n`)               :  Append a literal newline.
[r](f=a/2-b/2)       :  Repeat the resulting string a/2-b/2 times, assigning the result of that calculation to variable f.
+                    :Append.
(e=...)              :Assign to variable e...
(g=...)              :  Assign to variable g...
"#"[r](f)            :    # repeated f times.
+"*"[r](b)           :  Append * repeated b times.
+g+`\n`)             :  Append g and a literal newline.
[r](h=b/2-c/2)       :  Repeat the resulting string b/2-c/2 times, assigning the result of that calculation to variable h.
+(...)               :Append ...
g+                   :  g
(i=...)              :  Assign to variable i...
"*"[r](h)            :    * repeated h times.
+"@"[r](c)           :  @ repeated c times
+i+g+`\n`)           :  Append i, g and a literal newline.
[r](c)               :...repeated c times.
+e+d                 :Append e and d.


2

V70,44,42个字节

Àé#@aÄÀG@b|{r*ÀG@c|{r@òjdòÍ.“.
ç./æ$pYHP

在线尝试!

真可怕 真是的 好多了。仍然不是最短的,但至少有点像高尔夫球。

感谢@ nmjmcman101,节省了两个字节!

十六进制转储:

00000000: c0e9 2340 61c4 c047 4062 7c16 7b72 2ac0  ..#@a..G@b|.{r*.
00000010: 4740 637c 167b 7240 f26a 64f2 cd2e 932e  G@c|.{r@.jd.....
00000020: 0ae7 2e2f e624 7059 4850                 .../.$pYHP

您可以合并最后两行以节省两个字节。在线尝试!
nmjcman101

@ nmjcman101啊,好点。谢谢!
DJMcMayhem


1

Mathematica,49个字节

Print@@@Fold[#~CenterArray~{#2,#2}+1&,{{}},{##}]&

接受输入[c, b, a]。输出为a=1, b=2, c=3

怎么样?

Print@@@Fold[#~CenterArray~{#2,#2}+1&,{{}},{##}]&
                                                &  (* Function *)
        Fold[                        ,{{}},{##}]   (* Begin with an empty 2D array.
                                                      iterate through the input: *)
                                    &              (* Function *)
             #~CenterArray~{#2,#2}                 (* Create a 0-filled array, size
                                                      (input)x(input), with the array
                                                      from the previous iteration
                                                      in the center *)
                                  +1               (* Add one *)
Print@@@                                           (* Print the result *)

。@Jenny_mathy在问题:*函数可能返回字符串换行,字符串数组,或打印” Grid不成String,也不做它Print吧。
JungHwan闵

0

PHP> = 7.1,180字节

在这种情况下,我讨厌变量$以PHP 开头

for([,$x,$y,$z]=$argv;$i<$x*$x;$p=$r%$x)echo XYZ[($o<($l=$x-$a=($x-$y)/2)&$o>($k=$a-1)&$p>$k&$p<$l)+($o>($m=$k+$b=($y-$z)/2)&$o<($n=$l-$b)&$p>$m&$p<$n)],($o=++$i%$x)?"":"\n".!++$r;

PHP沙盒在线


在这种情况下,打印前的油漆要短得多。:D还是因为我$argv用作数组?你有尝试过吗?您是否尝试过单独的三元组?
泰特斯(Titus),

@Titus我不知道,但我计算策略更正无效奇数的输入
约尔格Hülsermann

0

Mathematica,173个字节

(d=((a=#1)-(b=#2))/2;e=(b-(c=#3))/2;z=1+d;x=a-d;h=Table["*",a,a];h[[z;;x,z;;x]]=h[[z;;x,z;;x]]/.{"*"->"#"};h[[z+e;;x-e,z+e;;x-e]]=h[[z+e;;x-e,z+e;;x-e]]/.{"#"->"@"};Grid@h)&

输入

[12,6,2]


0

Python 2,87个字节

a,_,_=t=input();r=~a
exec"r+=2;print sum(10**x/9*10**((a-x)/2)*(r*r<x*x)for x in t);"*a

在线尝试!

通过添加表格的数字来算术计算要打印的数字111100。有很多丑陋之处,可能还有改进的余地。


0

Java的8,265 252个字节

 a->b->c->{int r[][]=new int[a][a],x=0,y,t;for(;x<a;x++)for(y=0;y<a;r[x][y++]=0);for(t=(a-b)/2,x=t;x<b+t;x++)for(y=t;y<b+t;r[x][y++]=1);for(t=(a-c)/2,x=t;x<c+t;x++)for(y=t;y<c+t;r[x][y++]=8);String s="";for(int[]q:r){for(int Q:q)s+=Q;s+="\n";}return s;}

-13个字节,用数字替换字符。

绝对可以使用其他方法打高尔夫球。

说明:

在这里尝试。

a->b->c->{                         // Method with three integer parameters and String return-type
  int r[][]=new int[a][a],         //  Create a grid the size of `a` by `a`
      x=0,y,t;                     //  Some temp integers
  for(;x<a;x++)for(y=0;y<a;r[x][y++]=0);
                                   //  Fill the entire grid with zeros
  for(t=(a-b)/2,x=t;               //  Start at position `(a-b)/2` (inclusive)
      x<b+t;x++)                   //  to position `b+((a-b)/2)` (exclusive)
    for(y=t;y<b+t;r[x][y++]=1);    //   And replace the zeros with ones
  for(t=(a-c)/2,x=t;               //  Start at position `(a-c)/2` (inclusive)
      x<c+t;x++)                   //  to position `c+((a-b)/2)` (exclusive)
    for(y=t;y<c+t;r[x][y++]=8);    //   And replace the ones with eights
  String s="";                     //  Create a return-String
  for(int[]q:r){                   //  Loop over the rows
    for(int Q:q)                   //   Inner loop over the columns
      s+=Q;                        //    and append the result-String with the current digit
                                   //   End of columns-loop (implicit / single-line body)
    s+="\n";                       //   End add a new-line after every row
  }                                //  End of rows-loop
  return s;                        //  Return the result-String
}                                  // End of method

0

JavaScript(ES6),112

返回多行字符串的匿名函数。字符0,1,2

(a,b,c)=>eval("for(o='',i=-a;i<a;o+=`\n`,i+=2)for(j=-a;j<a;j+=2)o+=(i>=c|i<-c|j>=c|j<-c)+(i>=b|i<-b|j>=b|j<-b)")

少打高尔夫球

(a,b,c)=>{
  for(o='',i=-a;i<a;o+=`\n`,i+=2)
    for(j=-a;j<a;j+=2)
      o+=(i>=c|i<-c|j>=c|j<-c)+(i>=b|i<-b|j>=b|j<-b)
  return o
}  

var F=
(a,b,c)=>eval("for(o='',i=-a;i<a;o+=`\n`,i+=2)for(j=-a;j<a;j+=2)o+=(i>=c|i<-c|j>=c|j<-c)+(i>=b|i<-b|j>=b|j<-b)")

function update()
{
  var [a,b,c]=I.value.match(/\d+/g)
  O.textContent=F(a,b,c)
}

update()
  
a,b,c <input id=I value='10 6 2' oninput='update()'>
<pre id=O></pre>


0

PHP> = 5.6,121字节

for($r=A;$p?:$p=($z=$argv[++$i])**2;)$r[((--$p/$z|0)+$o=($w=$w?:$z)-$z>>1)*$w+$p%$z+$o]=_BCD[$i];echo chunk_split($r,$w);

在线运行-nr或对其进行测试

再次组合循环...我爱他们!

分解

for($r=A;                           # initialize $r (result) to string
    $p?:$p=($z=$argv[++$i])**2;)    # loop $z through arguments, loop $p from $z**2-1 to 0
    $r[((--$p/$z|0)+$o=
        ($w=$w?:$z)                     # set $w (total width) to first argument
        -$z>>1)*$w+$p%$z+$o]            # calculate position: (y+offset)*$w+x+offset
        =_BCD[$i];                      # paint allowed character (depending on $i)
echo chunk_split($r,$w);            # insert newline every $w characters and print
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.