输入: 从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
输入: 从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
Answers:
注意: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%2
与X
返回结果字符串,并且,因此,其是"X "
在所述第一,第三等线,以及" X"
对别人。
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
是字符串重复运算符。'a' x 3
产量'aaa'
。
(x=gets.to_i).times
保存三个字符。为什么突然对这首老歌重新产生兴趣?
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
}
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
}
}
}
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;}
"X ".charAt(i%n+i/n&1)
而不是((i%n+i/n)%2<1?"X":" ")
for(j=n=readline()|0;j--;)console.log(' X'.repeat(n).substr(j%2,n))
i,j;main(n){for(scanf("%d",&n);i++<n;puts(""))for(j=0;j<n;)putchar(i+j++&1?88:32);}
1 INPUTN:FORI=1TON;FORJ=1TON:IFI+JAND1THENPRINT" ";:GOTO3
2 PRINT"X";
3 NEXT:PRINT"":NEXT
on...go to
有条件地使用该命令,就像ON-(I+JAND1)GOTO3:?"X";:
它为零一样,它将落入以下语句,在这种情况下,如果(I + J AND 1) === 0
这样,它将打印X。这使您可以在每行中打包更多的语句并节省字节。
函数的字符数为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是输入。
("X "*n)sliding n take n map println
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)]
i,j;main(n){for(scanf("%d",&n);i<n;puts(""),i++)for(j=0;j<n;j++)putchar("X "[i+j&1]);}
{$[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.
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.
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);
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
█░█░█░█░█░█░█░
░█░█░█░█░█░█░█
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}
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.
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
([:{&' X'=/~@$&1 0)
$"1[:|.&'X '"+i.
Try it online!
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
for(;@$i++<$k=$argv[1];){for($j=0;$j++<$k;)echo($i+$j)%2?' ':'X';echo"\n";}