棋盘图案


18

输入: 从stdin或作为命令行参数输入一个小于80的正数。

输出:方形棋盘图案,输入数字的大小。暗场用字母“ X”表示,白场用空格表示。左上角的字段应为“ X”。

需要完整的程序。


例子:

输入 1

输出

X

输入 8

输出

X X X X 
 X X X X
X X X X 
 X X X X
X X X X 
 X X X X
X X X X 
 X X X X

1
我一直在寻找像“轻量级”这样的标签。
steenslag 2011年

1
我想整个程序完整吗?
JB

@JB:是的。我该如何制定呢?将“ to stdout”添加到所需的输出?
steenslag 2011年

1
只是说您想要一个完整的程序。您可能还需要指定命令行参数,以免与函数参数混淆。
JB

当您说右上时,您是说左上吗?如果没有,请更正输出输入8例如
彼得·泰勒

Answers:


5

Pyth,13个字符

注意:Pyth太新了,无法胜出。但是,那是一场有趣的高尔夫,我想我会分享。

VQ<*QX*d2N\XQ

在这里尝试。

怎么运行的:

                       Q = eval(input())
VQ                     for N in range(Q):
  <         Q                                                        [:Q]
   *Q                                    (Q*                        )
     X*d2N\X                                assign_at(" "*2, N, "X")

基本上,这用于X生成"X "" X"交替,然后重复该字符串Q时间,并采用其第一个Q字符。这是重复的Q次数。

请问该X功能的工作(在指定)?它采用原始字符串(" "在这种情况下),分配位置(N在这种情况下)和替换字符("X"在这种情况下)。由于Pyth的分配是模块化的,这在替换位置的空间N%2X返回结果字符串,并且,因此,其是"X "在所述第一,第三等线,以及" X"对别人。


...但是APL不是。感谢您的加油。想知道OP将重新接受...
亚当

“ Pyth太新了,没有资格获胜”我不理解这一点,并重新接受这一观点。
steenslag '16

1
@steenslag要说明,存在一个标准漏洞,即比问题更新的语言不符合要求。这是为了防止专门为应对特定挑战而设计的语言。当然,您可以自由地应对挑战。
isaacg

11

Golfscript-17个字符

~:N,{"X "N*>N<n}%

分析

~对于[0 ... N-1]的每个值,将输入转换
:N为变量N中的int 存储。重复“ X”,以给出N * 2个字符的字符串,并从循环索引开始采用子字符串... ...以N个字符结尾,然后在每个字符串的末尾添加换行符
,{...}
"X "N*
>
N<
n


5

佩尔(41) 40

for$i(1..$_){say substr" X"x$_,$i%2,$_}

Perl 5.10或更高版本,运行时perl -nE 'code'n以代码大小计)

样本输出:

$ perl -nE'for$i(1..$_){say substr" X"x 40,$i%2,$_}' <<<5
X X X
 X X
X X X
 X X
X X X
$ perl -nE'for$i(1..$_){say substr" X"x 40,$i%2,$_}' <<<8
X X X X
 X X X X
X X X X
 X X X X
X X X X
 X X X X
X X X X
 X X X X

“ x 40”中的“ x”是做什么的?
steenslag'3

2
@steenslag:x是字符串重复运算符。'a' x 3产量'aaa'
JB


3

Python,76个字符

n=input()
p='X '*n
print n/2*(p[:n]+'\n'+p[1:n+1]+'\n'),
if n&1:print p[:n]

3

Scala- 141 95个字符

var a=args(0).toInt
for(y<-1 to a;x<-1 to a)print((if((x+y)%2<1)"X"else" ")+("\n"*(x/a)take 1))

用法:scala filename N其中n是您对该程序的输入。


3

APL(16)

假设⎕IO=0(即零索引数组,这是一个设置)

' X'[=/¨2⊤¨⍳2⍴⎕]

说明:

  • ⍳2⍴⎕:读取数字N,并创建一个包含(0,0)至(N-1,N-1)的N×N矩阵。
  • 2⊤¨:获取矩阵中每个数字的最低有效位。(所以现在我们有(0,0),(0,1),(0,0)...(1,0),(1,1),(1,0)...)
  • =/¨:对于每对,请查看两个数字是否相等。(现在我们有1 0 1 0 1 0 ...)
  • ' X'[... ]:对每个0放置一个空格,对每个1放置一个X。


2

蟒蛇

48个字符

编辑: Kinda错误的...在末尾有一个额外的空间...但是那是不可见的。如果将空格更改为“ O”(或任何非空白char),则将其修改[i%2:n][i%2:n+i%2]。为正确的版本。

n=input()
i=0;
while i<n:print('X '*n)[i%2:n];i+=1

2

C ++-253个混淆字符

#include <iostream.h>
int main(int i,char*c[]=0)
{
  char a=i,b=i>>8;i&32512?((i>>16&255)<a)?(cout<<b)?main((i^30720)+65536):0:(cout<<endl)?(((b=(i>>24)+1)<a)?main((i&2130706559)+((b&1)?16785408:16799744)):0):0:main((i>=2?atoi(1[c]):8)|22528);
}

1
我喜欢所有的魔术数字。
乔伊·亚当斯

2

JavaScript 169

function b(w){var i=w,j=w,r='';while(i--){while(j--){if((i+j)%2)r+=' ';else r+='X'}j=w;r+="\n"}return r}do{n=parseInt(prompt('Number'))}while(isNaN(n)||n<1);alert(b(n));

我很确定您可以将转换为+'1'而不是parseInt('1')。不会给您一个整数,但我认为在这里让它成为整数并不重要,是吗?
盖伊

2

k(26个字符)

26对于裸功能:

{-1',/x#',x#'("X ";" X");}

或另外7个从stdin接受输入

{-1',/x#',x#'("X ";" X");}"I"$0:0

2

重击:60个字符

yes X|fmt -w80|paste -d '' <(yes '
 ') -|head -$1|cut -c1-$1

表大小作为命令行参数传递,例如bash chesstable.sh 8


2

Java 10,lambda函数,92 87 84字节

n->{for(int i=0;i<n*n;)System.out.print(((i%n+i/n)%2<1?"X":" ")+(++i%n<1?"\n":""));}

在这里在线尝试。

多亏了ceilingcat打高尔夫球4字节,还要感谢Kevin Cruijssen再打高尔夫球3 个字节。

非高尔夫版本:

n -> { // void lambda taking an int as argument
    for(int i = 0; i < n*n; ) // loop over the entire square
            System.out.print(((i%n + i/n) % 2 < 1 ? "X" : " ") // print an 'X' or a space depending on which line&column we're on
               + (++i % n < 1 ? "\n" : "")); // print a newline at the end of a line
}

Java的8,全程式,155个 139字节

interface M{static void main(String[]a){int i=0,n=new Byte(a[0]);for(;i<n*n;)System.out.print(((i%n+i/n)%2<1?"X":" ")+(++i%n<1?"\n":""));}}

在这里在线尝试。

非高尔夫版本:

interface M {
    static void main(String[] a) {
        int i = 0, // iterator variable for the loop
            n = new Byte(a[0]); // take the argument and convert it to an int
        for(; i < n*n; ) // loop over the entire square
            System.out.print( ((i%n + i/n) % 2 < 1 ? "X" : " ") // print an 'X' or a space depending on which line&column we're on
                             +(++i % n < 1 ? "\n" : "") ); // print a newline at the end of a line
        }
    }
}

@ceilingcat谢谢!我设法减少了一个字节,并将您的方法也应用到了我的整个程序中。16字节保存在那里。
OOBalance '18

做两个s+=比括号短2个字节:n->{var s="";for(int i=0;i<n*n;s+=++i%n<1?"\n":"")s+=(i%n+i/n)%2<1?"X":" ";return s;}
Kevin Cruijssen

或1点字节关闭(84个)通过直接印刷(用括号再次XD): n->{for(int i=0;i<n*n;)System.out.print(((i%n+i/n)%2<1?"X":" ")+(++i%n<1?"\n":""));}在线尝试。
凯文·克鲁伊森

建议"X ".charAt(i%n+i/n&1)而不是((i%n+i/n)%2<1?"X":" ")
ceilingcat


2

APL(Dyalog扩展),12 字节SBCS

匿名默认前缀功能。需要⎕IO←0(从零开始的索引)。

'X '⊇⍨2|⍳+⍀⍳

在线尝试!

ɩ0 …n–1

+⍀ 加上水平和垂直的表格:

ɩ0 …n–1

2| 除数除以二

'X '⊇⍨ 使用该矩阵索引到字符串


2

Scala,68个字节

  def^(n:Int)=for(a<-1 to n)println(("x_"*n).substring(n%2+1,n+n%2+1))

要么

  def^(n:Int)=for(a<-1 to n)println(("x_"*n).substring(a%2+1).take(n))

在线尝试!


2

Brainfuck,140个字节

-[+[+<]>>+]>++++[<++++++++>-]<<<<<<,[->+>+>+<<<]>>[->[->.<[->>.>]<<<]<[<<<]>>>>>[-<+>]>[-<+>]<<[->>+<<]<<[-<+>]<[->+>>+<<<]++++++++++.[-]>>]

2

Javascript,67个字节

for(j=n=readline()|0;j--;)console.log(' X'.repeat(n).substr(j%2,n))

在线尝试

C,83字节

i,j;main(n){for(scanf("%d",&n);i++<n;puts(""))for(j=0;j<n;)putchar(i+j++&1?88:32);}

在线尝试

基本C64,89个字节

1 INPUTN:FORI=1TON;FORJ=1TON:IFI+JAND1THENPRINT" ";:GOTO3
2 PRINT"X";
3 NEXT:PRINT"":NEXT

enter image description here


您可以on...go to有条件地使用该命令,就像ON-(I+JAND1)GOTO3:?"X";:它为零一样,它将落入以下语句,在这种情况下,如果(I + J AND 1) === 0这样,它将打印X。这使您可以在每行中打包更多的语句并节省字节。
Shaun Bebbers


2

Scala,40和54

函数的字符数为40,完整程序的字符数为54。

仅提供功能主体的解决方案是:

("X "*n)sliding n take n foreach println

在线尝试

 

提供完整程序的解决方案是:

val n=readInt;("X "*n)sliding n take n foreach println

您可以使用以下命令行运行它。

scala -e 'val n=readInt;("X "*n)sliding n take n foreach println' <<< 8

其中8是输入。


1
欢迎使用PP&CG,并且是一个不错的第一答案。有一个名为“ 在线试用”的不错的网站,使您可以轻松进行字节计数和共享运行。查看其他Scala答案以查看示例。不必介意,只要拥有就可以了。
维斯卡(Veskah)

感谢@Veskah的建议。
jseteny

好的解决方案,将其更改为函数并使用map,您将获得34个字符:("X "*n)sliding n take n map println
pme

@pme感谢您的建议,但是如果我用map替换foreach则没有输出。但是,我将其更改为您建议的功能。
jseteny

您说得对-对不起,地图很懒;(。
pme

1

Python - 127 characters

from sys import*
r=stdout.write
s=int(raw_input())
[[r((x+y+1)%2 and"x"or" ")for x in range(s)]and r("\n")for y in range(s)]


1

Q, 33

{$[1=x mod 2;x;x-1]cut(x*x)#"X "}

{(x;x-1-x mod 2)#"X "} for 22... ah no, has the same bug as yours - doesnt have 4 X's on the odd rows for input 8.
streetster

1

Ruby 58

i=ARGV[0].to_i
1.upto(i*i){|n|print n%i==0?"\n":' x'[n%2]}

1

PHP - 136 chars (without whitespace)

Allows for x and y function input.

Also supports odd inputs now.

If you style the output to have 0.65 em line-height and change this ▒█ and █░ to □■ and ■□ then it comes out looking like a real (square) chessboard.

Code:

function gen_cb($x,$y)
{
$c=0;
$y*=2;
for($i=0;$i<$y;$i++){
for($j=0;$j<$x;$j++){
echo $c%2==0 ? "░█" : "█░";
}
echo "<br/>";
$c++;
}
}
gen_cb(7,7);

Output:

░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█

Does it work for boards with an odd number of squares per side?
Gareth

@Gareth Now it does
Event_Horizon

1

PHP, 87

for($x=0;$x<$m=$argv[1];$x++){echo"\n";for($y=0;$y<$m;$y++)echo($y^($x%2))%2?' ':'X';}

1

CJam, 18 bytes

I probably could have just ported the GolfScript answer, but here is a different approach. (And CJam is not eligible for winning anyway.)

l~,_f{f{+2%S'X?}N}

Test it here.

The idea is to iterate over the 2D grid with x and y indices on the stack, using the f{f{...}} trick. Given x and y, we can simply determine black and white as (x+y)%2 and use that to pick between the character X and a string containing a space.


1

J, 21 chars

J was missing too.

   ([:u:32+56*=/~@$&1 0) 5
X X X
 X X 
X X X
 X X 
X X X

Previous, 22 chars:

Char codes from mod2 pattern of row # + column #:

   ([:u:88-56*2&|@+/~@i.) 5

-2 Bytes: ([:{&' X'=/~@$&1 0)
Bolce Bussiere

16 bytes $"1[:|.&'X '"+i. Try it online!
Jonah

1

VB.net, 161

Module C
   Sub Main()
     Dim n As Integer
     If Integer.TryParse(Console.ReadLine,n) Then
     For x=1To n
        For y=1To n
          Console.Write("* "((x+y)Mod 2))
        Next
        Console.WriteLine()
      Next
     End If
    End Sub
End Module

1

PHP, 75 bytes

for(;@$i++<$k=$argv[1];){for($j=0;$j++<$k;)echo($i+$j)%2?' ':'X';echo"\n";}
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.