English 中文(简体)
高尔夫球:造物
原标题:Code Golf: Build Me an Arc
Locked. This question and its answers are locked because the question is off-topic but has historical significance. It is not currently accepting new answers or interactions.

Challenge

接受<代码>X-Y R标准输入的按特性分列的最短方案,保证如下:

  • R is a non-negative decimal number less than or equal to 8
  • X and Y are non-negative angles given in decimal as multiples of 45° (0, 45, 90, 135, etc.)
  • X is less than Y
  • Y is not 360 if X is 0

并按标准产出编制ASCII“arc”从角开始的X到尾角的Y

  • The vertex of the arc is represented by o
  • Angles of 0 and 180 are represented by -
  • Angles of 45 and 225 are represented by /
  • Angles of 90 and 270 are represented by |
  • Angles of 135 and 315 are represented by
  • The polygonal area enclosed by the two lines is filled with a non-whitespace character.

如果投入无效,则不需要该方案产生有意义的产出。 允许使用任何语言的解决办法,当然是专门为这一挑战而写的语言,或不公平地利用外部用途的语言。 产出中允许的外相横向和纵向白色空间is,条件是产出的格式仍然正确。

www.un.org/Depts/DGACM/index_spanish.htm 幸福!

Numerous Examples

投入:

0-45 8

产出:

        /
       /x
      /xx
     /xxx
    /xxxx
   /xxxxx
  /xxxxxx
 /xxxxxxx
o--------

投入:

0-135 4

产出:

xxxxxxxx
 xxxxxxx
  xxxxxx
   xxxxx
    o----

投入:

180-360 2

产出:

--o--
xxxxx
xxxxx

投入:

45-90 0

产出:

o

投入:

0-315 2

产出:

xxxxx
xxxxx
xxo--
xxx
xxxx
最佳回答

Perl, 235 211 225 211 207 196 179 177 175 168 160 156 146 chars

<>=~/-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),$/}

www.un.org/Depts/DGACM/index_spanish.htm

Perl using say feature, 161 149 139 chars

$ 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} 

www.un.org/Depts/DGACM/index_spanish.htm

Perl without trailing newline, 153 143 chars

<>=~/-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}

www.un.org/Depts/DGACM/index_spanish.htm Original version commented:

$_=<>;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;

www.un.org/Depts/DGACM/index_spanish.htm EDIT 1: Inlined sub, relational and equality operators return 0 or 1. www.un.org/Depts/DGACM/index_spanish.htm EDIT 2: Added version with comments. www.un.org/Depts/DGACM/index_spanish.htm EDIT 3: Fixed enclosing line at 360º. Char count increased significantly. www.un.org/Depts/DGACM/index_spanish.htm EDIT 4: Added a shorter version, bending the rules. www.un.org/Depts/DGACM/index_spanish.htm EDIT 5: Smarter fix for the 360º enclosing line. Also, use a number as fill. Both things were obvious. Meh, I should sleep more :/ www.un.org/Depts/DGACM/index_spanish.htm EDIT 6: Removed unneeded m from match operator. Removed some semicolons. www.un.org/Depts/DGACM/index_spanish.htm EDIT 7: Smarter regexp. Under 200 chars! www.un.org/Depts/DGACM/index_spanish.htm EDIT 8: Lots of small improvements:

  • Inner for loop -> map (1 char)
  • symbol array from split string -> qw (3 chars)
  • inlined symbol array (6 chars, together with the previous improvement 9 chars!)
  • Logical or -> bitwise or (1 char)
  • Regexp improvement (1 char)
  • Use arithmethic for testing negative angles, inspired by Jacob s answer (5 chars)

www.un.org/Depts/DGACM/index_spanish.htm EDIT 9: A little reordering in the conditional operators saves 2 chars. www.un.org/Depts/DGACM/index_spanish.htm EDIT 10: Use barewords for characters. www.un.org/Depts/DGACM/index_spanish.htm EDIT 11: Moved print inside of loop, inspired by Lowjacker s answer. www.un.org/Depts/DGACM/index_spanish.htm EDIT 12: Added version using say. www.un.org/Depts/DGACM/index_spanish.htm EDIT 13: Reuse angles characters for fill character, as Gwell s answer does. Output isn t as nice as Gwell s though, that would require 5 additional chars :) Also, .. operator doen t need parentheses. www.un.org/Depts/DGACM/index_spanish.htm EDIT 14: Apply regex directly to <>. Assign range operator to a variable, as per Adrian s suggestion to bta s answer. Add version without the final newline. Updated say version. www.un.org/Depts/DGACM/index_spanish.htm EDIT 15: More inlining. map{block}@a -> map expr,@a.

问题回答

Lua, 259 characters

略微滥用<条码>非白色空间特性条款,以产生一种令人厌恶的显示,更重要的是避免中风。

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----
--//|\--
////|\\
///|||\
///|||\

Able to handle odd angles

投入:15-75 8

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

MATLAB, 188 chars :)

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

Commented code:

%%# 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)

Mathematica 100 Chars

由于图形过于完美:

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

Invoke with f[30-70 5]

成果

http://a.imageshack.us/img80/4294/angulosgolf.png”

http://a.imageshack.us/img59/7892/angulos2.png”

Note

The SetAttributes[f, HoldAll];

之所以需要,是因为投入

    f[a-b c] 

否则将被解释为

    f[(a-b*c)]

GNU BC, 339 chars

www.un.org/spanish/ga/president

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

Ruby, 292 276 186 chars

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
}

我确信,有人离开那里,比我能想象得更多睡觉......

<><>><>>>> 储存范围到中间变量(Athanks Adrian),使用中链而不是CLI params(为Jon提供澄清),消除了有利于直接产出的阵列,固定的泡沫,如果终止360英寸铁线的角,去除一些不需要的母轮-,使用分管而不是<代码>。

Ruby, 168 characters

<>Requestong>Require.1.9 to work

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
}

Perl - 388 characters

由于提出挑战不公,我不得不自己解决,这里的解决办法是,利用扼杀替代功能,而不是三管齐下的职能,并大力利用你的友好邻里伯尔的能力,把空话当作扼杀。 为了独一无二的目的,它必然需要很长一段时间,但也许会很有意义:

($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$_."
";
$b.=M x$t.F.N x$_.G.O x$_.H.P x$t."
"}
$_=$a.E x$r.o.A x$r."
".$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

所有新路线都是任择性的。 这简直是:

  • Grab user input.
  • Build the top ($a) and bottom ($b) parts of the pattern.
  • Build the complete pattern ($_).
  • Define a sub A to get the fill character for an angle.
  • Define a sub B to get the fill character for a region.
  • Build an array (@a) of substitution characters using A and B.
  • Perform the substitution and print the results.

生成格式见R=4:

DKKKCJJJB
LDKKCJJBI
LLDKCJBII
LLLDCBIII
EEEEoAAAA
MMMFGHPPP
MMFNGOHPP
MFNNGOOHP
FNNNGOOOH

A-H 页: 1

(令人信服的是,这可能还会进一步扩大。) <代码>@a 在作为一份清单书写时给我不正确的产出,大概与如何填写<条码><>>>>> > <> 代码>_。

C# - 325 319 chars

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?"
":""));}}

新路线并不重要。

http://www.un.org。

45-180 8
||||||||////////
\|||||||///////
\||||||//////
\\|||||/////
\\||||////
\\\|||///
\\\||//
\\\\|/
--------o
135-360 5

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

Java - 304 chars


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?"":"
"));}}

可读文本:

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?"":"
"));
 }
}

C (902 byte)

这只字不提使用三角元功能(如原来的千字节),因此,它“变数”。 无论如何,这是我首次提交的刚果法典:

#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);}

此外,<代码>#define 相当粗略地看着,但是,它们节省了约200个用tes,因此我保留了它们。 它是有效的ANSI C89/C90,并用很少的警告汇编(两份关于atoiputs和两份关于main的附后形式)。





相关问题
XML-RPC Standard and XML Data Type

I was looking at XML-RPC for a project. And correct me if I m wrong, but it seems like XML-RPC has no XML datatype. Are you supposed to pass as a string? or something else? Am I missing something? ...

Is it exists any "rss hosting" with API for creating feeds

I am creating a desktop app that will create some reports. I want to export these reports as RSS or ATOM feeds. I can easily create feeds with Rome lib for Java. But I have no idea how to spread them. ...

Improving Q-Learning

I am currently using Q-Learning to try to teach a bot how to move in a room filled with walls/obstacles. It must start in any place in the room and get to the goal state(this might be, to the tile ...

High-traffic, Highly-secure web API, what language? [closed]

If you were planning on building a high-traffic, very secure site what language would you use? For example, if you were planning on say building an authorize.net-scale site, that had to handle tons ...

Def, Void, Function?

Recently, I ve been learning different programming langages, and come across many different names to initalize a function construct. For instance, ruby and python use the def keyword, and php and ...

热门标签