20

挑战

接受格式标准输入的按字符计数最短的程序X-Y R,具有以下保证:

  • R是小于或等于 8 的非负十进制数
  • X并且Y是以十进制形式给出的非负角度,以 45° 的倍数(04590135等)
  • X小于Y
  • Y不是360如果X0

并在标准输出上产生一个从半径的起始角度X到结束角度的 ASCII“弧” ,其中:YR

  • 弧的顶点表示为o
  • 的角度0180表示为-
  • 的角度45225表示为/
  • 的角度90270表示为|
  • 的角度135315表示为\
  • 由两条线包围的多边形区域用非空白字符填充。

如果给定无效输入,则程序不需要产生有意义的输出。任何语言的解决方案都是允许的,当然除了专门为此挑战编写的语言,或者不公平地使用外部实用程序的语言。如果输出格式保持正确,则输出允许存在无关的水平和垂直空格。

打高尔夫球快乐!

许多例子

输入:

0-45 8

输出:

        /
       /X
      /xx
     /xxx
    /xxxx
   /xxxxx
  /xxxxxx
 /xxxxxx
o--------

输入:

0-135 4

输出:

\xxxxxxxx
 \xxxxxxx
  \xxxxxx
   \xxxxx
    o----

输入:

180-360 2

输出:

--o--
xxxxx
xxxxx

输入:

45-90 0

输出:

输入:

0-315 2

输出:

xxxxx
xxxxx
xxo--
xxx\
xxxx\
4

12 回答 12

15

Perl,235 211 225 211 207 196 179 177 175 168 160 156 146 个字符

<>=~/-\d+/;for$y(@a=-$'..$'){print+(map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a),$/}


Perl 使用说功能,161 149 139 个字符

$ echo -n '<>=~/-\d+/;for$y(@a=-$'"'"'..$'"'"'){say map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}' | wc -c
139
$ perl -E '<>=~/-\d+/;for$y(@a=-$'"'"'..$'"'"'){say map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}'


Perl 没有尾随换行符,153 143 个字符

<>=~/-\d+/;for$y(@a=-$'..$'){print$/,map$_|$y?!($t=8*($y>0)+atan2(-$y,$_)/atan2 1,1)&-$&/45==8|$t>=$`/45&$t<=-$&/45?qw(- / | \\)[$t%4]:$":o,@a}


原版评论:

$_=<>;m/(\d+)-(\d+) (\d+)/;$e=$1/45;$f=$2/45; # parse angles and radius, angles are 0-8
for$y(-$3..$3){                               # loop for each row and col
    for$x(-$3..$3){
            $t=atan2(-$y,$x)/atan2 1,1;   # angle of this point
            $t+=8if($t<0);                # normalize negative angles
            @w=split//,"-/|\\"x2;         # array of ASCII symbols for enclosing lines
            $s.=!$x&&!$y?"o":$t==$e||$t==$f?$w[$t]:$t>$e&&$t<$f?"x":$";
            # if it's origin -> "o", if it's enclosing line, get symbol from array
            # if it's between enclosing angles "x", otherwise space
    }
    $s.=$/;
}
print$s;


编辑 1:内联子、关系和相等运算符返回 0 或 1。
编辑 2:添加了带有注释的版本。
编辑 3:将封闭线固定在 360º。字符数显着增加。
编辑 4:添加了一个较短的版本,改变了规则。
编辑 5:更智能地修复 360º 封闭线。此外,使用数字作为填充。这两件事都很明显。嗯,我应该多睡点:/
编辑 6:m从匹配运算符中删除不需要的。删除了一些分号。
编辑 7:更智能的正则表达式。不到 200 个字符!
编辑 8:许多小的改进:

  • 内部 for 循环 -> 映射(1 个字符)
  • 来自字符串的符号数组split-> qw(3 个字符)
  • 内联符号数组(6个字符,加上之前改进的9个字符!)
  • 逻辑或 -> 按位或(1 个字符)
  • 正则表达式改进(1 个字符)
  • 受雅各布的回答启发,使用算术测试负角(5 个字符)


编辑 9:在条件运算符中进行一些重新排序可以节省 2 个字符。
编辑 10:对字符使用裸词。
编辑 11:受 Lowjacker 的回答启发,在循环内移动打印。
编辑 12:使用say.
编辑 13:重用角度字符作为填充字符,就像 Gwell 的回答一样。输出不如 Gwell 的好,这需要 5 个额外的字符 :) 此外,.. 运算符不需要括号。
编辑 14:将正则表达式直接应用于 <>。根据 Adrian 对 bta 答案的建议,将范围运算符分配给变量。添加没有最终换行符的版本。更新say版本。
编辑 15:更多内联。映射{块}@a -> 映射表达式,@a。

于 2010-07-29T21:19:12.997 回答
12

Lua,259 个字符

稍微滥用该non-whitespace character条款以产生令人眼花缭乱的展示,更重要的是节省笔画。

m=math i=io.read():gmatch("%d+")a=i()/45 b=i()/45 r=i()for y=r,-r,-1 do for x=-r,r do c=m.atan2(y,x)/m.pi*4 c=c<0 and c+8 or c k=1+m.modf(c+.5)io.write(x==0 and y==0 and'o'or c>=a and c<=b and('-/|\\-/|\\-'):sub(k,k)or c==0 and b==8 and'-'or' ')end print()end

输入:45-360 4

\\\|||///
\\\|||//
\\\\|//  
--\\|/   
----o----
--//|\\--
////|\\\\
///|||\\\
///|||\\\

能够处理奇怪的角度

输入:15-75 8

           |/////
          |//////
          |//////
          |//////
          ///////
         |//////-
         ////---
         //-     
        ○        
                 
                 
                 
                 
                 
                 
                 
                 
于 2010-07-31T05:55:37.683 回答
8

MATLAB,188 个字符 :)

input '';[w x r]=strread(ans,'%d-%d%d');l='-/|\-/|\-';[X Y]=meshgrid(-r:r);T=atan2(-Y,X)/pi*180;T=T+(T<=0)*360;T(T>w&T<x)=-42;T(T==w)=-l(1+w/45);T(T==x)=-l(1+x/45);T(r+1,r+1)=-'o';char(-T)

注释代码:

%%# Get the string variable (enclose in quotes, e.g. '45-315 4')
input ''
%%# Extract angles and length
[w x r]=strread(ans,'%d-%d%d');
%%# Store characters
l='-/|\-/|\-';
%%# Create the grid
[X Y]=meshgrid(-r:r);
%%# Compute the angles in degrees
T=atan2(-Y,X)/pi*180;
%%# Get all the angles
T=T+(T<=0)*360;
%# Negative numbers indicate valid characters
%%# Add the characters
T(T>w&T<x)=-42;
T(T==w)=-l(1+w/45);
T(T==x)=-l(1+x/45);
%%# Add the origin
T(r+1,r+1)=-'o';
%%# Display
char(-T)
于 2010-07-30T07:09:27.833 回答
8

Mathematica 100 个字符

由于图形太完美而退出竞争:)

  f[x_-y_ z_]:=Graphics@Table[
                 {EdgeForm@Red,Disk[{0,0},r,{x °,y °}],{r,z,1,-1}]
                 SetAttributes[f,HoldAll]

使用 f[30-70 5] 调用

结果

替代文字 http://a.imageshack.us/img80/4294/angulosgolf.png

替代文字 http://a.imageshack.us/img59/7892/angulos2.png

笔记

SetAttributes[f, HoldAll];

是需要的,因为输入

    f[a-b c] 

否则被解释为

    f[(a-b*c)]
于 2010-08-02T02:25:03.567 回答
7

GNU BC,339 个字符

Gnu bc 因为read(),else和逻辑运算符。

scale=A
a=read()/45
b=read()/45
c=read()
for(y=c;y>=-c;y--){for(x=-c;x<=c;x++){if(x==0)if(y<0)t=-2else t=2else if(x>0)t=a(y/x)/a(1)else if(y<0)t=a(y/x)/a(1)-4else t=a(y/x)/a(1)+4
if(y<0)t+=8
if(x||y)if(t==a||t==b||t==b-8){scale=0;u=(t%4);scale=A;if(u==0)"-";if(u==1)"/";if(u==2)"|";if(u==3)"\"}else if(t>a&&t<b)"x"else" "else"o"};"
"}
quit
于 2010-07-30T21:15:30.780 回答
7

MATLAB 7.8.0 (R2009a) - 168 163 162 个字符

Jacob 的回答开始,并受到gwell 使用任何非空白字符填充弧线的启发,我管理了以下解决方案:

[w x r]=strread(input('','s'),'%d-%d%d');
l='o -/|\-/|\-';
X=meshgrid(-r:r);
T=atan2(-X',X)*180/pi;
T=T+(T<=-~w)*360;
T(T>x|T<w)=-1;
T(r+1,r+1)=-90;
disp(l(fix(3+T/45)))

还有一些测试输出:

>> arc
0-135 4
\||||////
 \|||///-
  \||//--
   \|/---
    o----

我可以通过删除对 的调用将其进一步减少到156 个字符disp,但这会ans =在输出之前添加一个额外的字符(这可能违反输出格式规则)。

尽管如此,我还是觉得有一些方法可以进一步减少这种情况。;)

于 2010-08-03T04:19:35.933 回答
6

红宝石,292 276 186 个字符

x,y,r=gets.scan(/\d+/).map{|z|z.to_i};s=(-r..r);s.each{|a|s.each{|b|g=Math::atan2(-a,b)/Math::PI*180/1%360;print a|b==0?'o':g==x||g==y%360?'-/|\\'[g/45%4].chr: (x..y)===g ?'*':' '};puts}

格式更好的版本:

x, y, r = gets.scan(/\d+/).map{|z| z.to_i}
s = (-r..r)
s.each {|a|
    s.each {|b|
        g = (((Math::atan2(-a,b) / Math::PI) * 180) / 1) % 360
        print ((a | b) == 0) ? 'o' :
            (g == x || g == (y % 360)) ? '-/|\\'[(g / 45) % 4].chr :
                ((x..y) === g) ? '*' : ' '
    }
    puts
}

我敢肯定,比我睡得更多的人可以更浓缩这一点……

编辑 1:if将内部循环中的语句切换到嵌套? :运算符

编辑 2:将范围存储到中间变量(感谢 Adrian),使用标准输入而不是 CLI 参数(感谢 Jon 的澄清),消除了数组以支持直接输出,修复了 360 度的结束角度不会显示一条线的错误,删除了一些不需要的括号,使用除法而不是四舍五入.round,使用模数而不是条件加法

于 2010-07-30T01:32:34.360 回答
4

红宝石,168 个字符

需要 Ruby 1.9 才能工作

s,e,r=gets.scan(/\d+/).map &:to_i;s/=45;e/=45;G=-r..r;G.map{|y|G.map{|x|a=Math.atan2(-y,x)/Math::PI*4%8;print x|y!=0?a==s||a==e%8?'-/|\\'[a%4]:a<s||a>e ?' ':8:?o};puts}

可读版本:

start, _end, radius = gets.scan(/\d+/).map &:to_i
start /= 45
_end /= 45

(-radius..radius).each {|y|
    (-radius..radius).each {|x|
        angle = Math.atan2(-y, x)/Math::PI * 4 % 8
        print x|y != 0 ? angle==start || angle==_end%8 ? '-/|\\'[angle%4] : angle<start || angle>_end ? ' ' : 8 : ?o
    }
    puts
}
于 2010-07-30T16:18:56.763 回答
3

Perl - 388 个字符

由于提出我自己无法解决的挑战是不公平的,因此这里有一个使用字符串替换而不是三角函数的解决方案,并大量利用您友好的邻居 Perl 将裸字视为字符串的能力。它必然有点长,但为了独特性,也许很有趣:

($x,$y,$r)=split/\D/,<>;for(0..$r-1){$t=$r-1-$_;
$a.=L x$_.D.K x$t.C.J x$t.B.I x$_."\n";
$b.=M x$t.F.N x$_.G.O x$_.H.P x$t."\n"}
$_=$a.E x$r.o.A x$r."\n".$b;$x/=45;$y/=45;$S=' ';
sub A{$v=$_[0];$x==$v||$y==$v?$_[1]:$x<$v&&$y>$v?x:$S}
sub B{$x<=$_[0]&&$y>$_[0]?x:$S}
@a=!$x||$y==8?'-':$S;
push@a,map{A$_,'\\'.qw(- / | \\)[$_%4]}1..7;
push@a,!$x?x:$S,map{B$_}1..7;
eval"y/A-P/".(join'',@a)."/";print

所有换行符都是可选的。这很简单:

  • 获取用户输入。
  • 构建图案的顶部 ( $a) 和底部 ( $b) 部分。
  • 构建完整的模式 ( $_)。
  • 定义 asub A以获取角度的填充字符。
  • 定义 asub B以获取区域的填充字符。
  • 使用and构建一个@a替换字符数组 ( ) 。AB
  • 执行替换并打印结果。

生成的格式如下所示,对于R= 4:

DKKKCJJJB
LDKKCJJBI
LLDKCJBII
LLLDCBIII
EEEEoAAA
MMMFGHPPP
MMFNGOHPP
MFNNGOOHP
FNNNGOOH

其中A-H表示角度并I-P表示区域。

(诚​​然,这可能会进一步打高尔夫球。@a当写成一个列表时,操作给了我不正确的输出,可能与如何map使用$_.)

于 2010-07-30T16:12:51.373 回答
3

C# - 325 319 个字符

using System;class P{static void Main(){var s=Console.ReadLine().Split(' ');
var d=s[0].Split('-');int l=s[1][0]-48,x,y,r,a=int.Parse(d[0]),b=int.Parse(d[1]);
for(y=l;y>=-l;y--)for(x=-l;x<=l;)Console.Write((x==0&&y==0?'o':a<=(r=((int)
(Math.Atan2(y,x)*57.3)+360)%360)&&r<b||r==b%360?
@"-/|\"[r/45%4]:' ')+(x++==l?"\n":""));}}

换行不重要。

样本输入/输出

45-180 8
\||||||||////////
\\|||||||///////
\\\||||||//////
\\\\|||||/////
\\\\\||||////
\\\\\\|||///
\\\\\\\|||//
\\\\\\\\|/
--------o
135-360 5
\
\\
\\\
\\\\
\\\\\
--o-----
----/|\\\\\
---//||\\\\
--///|||\\\
-////||||\\
/////||||||\
于 2010-08-11T10:14:20.673 回答
2

Java - 304 个字符


class A{public static void main(String[]a){String[]b=a[0].split("-");int e=new Integer(b[1]),r=new Integer(a[1]),g,x,y=r;for(;y>=-r;y--)for(x=-r;x<=r;)System.out.print((x==0&y==0?'o':new Integer(b[0])<=(g=((int)(Math.atan2(y,x)*57.3)+360)%360)&g<e|g==e%360?"-/|\\".charAt(g/45%4):' ')+(x++<r?"":"\n"));}}

更易读的版本:

class A{
 public static void main(String[]a){
  String[]b=a[0].split("-");
  int e=new Integer(b[1]),r=new Integer(a[1]),g,x,y=r;
  for(;y>=-r;y--)for(x=-r;x<=r;)System.out.print((
   x==0&y==0
    ?'o'
    :new Integer(b[0])<=(g=((int)(Math.atan2(y,x)*57.3)+360)%360)&g<e|g==e%360
     ?"-/|\\".charAt(g/45%4)
     :' '
   )+(x++<r?"":"\n"));
 }
}
于 2010-08-21T23:43:58.977 回答
2

C (902 字节)

这不使用三角函数(就像原来的 perl 版本一样),所以它相当“臃肿”。无论如何,这是我的第一个代码高尔夫提交:

#define V(r) (4*r*r+6*r+3)
#define F for(i=0;i<r;i++)
#define C ;break;case
#define U p-=2*r+2,
#define D p+=2*r+2,
#define R *++p=
#define L *--p=
#define H *p='|';
#define E else if
#define G(a) for(j=0;j<V(r)-1;j++)if(f[j]==i+'0')f[j]=a;
#define O(i) for(i=0;i<2*r+1;i++){
main(int i,char**v){char*p,f[V(8)];
int j,m,e,s,x,y,r;p=*++v;x=atoi(p);while(*p!=45)p++;
char*h="0123";y=atoi(p+1);r=atoi(*++v);
for(p=f+2*r+1;p<f+V(r);p+=2*r+2)*p=10;
*(p-2*r-2)=0;x=x?x/45:x;y/=45;s=0;e=2*r;m=r;p=f;O(i)O(j)
if(j>e)*p=h[0];E(j>m)*p=h[1];E(j>s)*p=h[2];else*p=h[3];p++;}
if(i+1==r){h="7654";m--;e--;}E(i==r){s--;}E(i>r){s--;e++;}
else{s++;e--;}p++;}for(p=f+V(r)/2-1,i=0;i<r;i++)*++p=48;
for(i=0;i<8;i++)if(i>=x&&i<y){G(64);}else G(32);
y=y==8?0:y;q:p=f+V(r)/2-1;*p='o';switch(x){
C 0:F R 45 C 1:F U R 47 C 2:F U H C 3:F U L 92
C 4:F L 45 C 5:F D L 47 C 6:F D H C 7:F D R 92;}
if(y!=8){x=y;y=8;goto q;}puts(f);}

此外,#defines 看起来相当丑陋,但它们节省了大约 200 个字节,所以无论如何我都保留了它们。它是有效的 ANSI C89/C90 并且编译时警告很少(两个关于atoiputs两个关于残缺形式的main)。

于 2010-08-28T15:28:59.940 回答