发射一个炮弹,使它在飞行的第一次眨眼时通过N树梢上升,在第二次眨眼时由N-1树顶上升,直到到达轨道的最高点。然后,它开始下降1,2,等等,每眨眼树梢,直到它击中地面。同时,炮弹水平移动,速度为1树梢/眼。
你的任务是用英语字母表中的连续字母画出轨迹。如果您的字母用完了,请从'A'重新开始。编写函数或程序。输入是整数N (1≤N≤15)。输出可以是任何合理形式的字符矩阵,例如换行符分隔的字符串或字符串列表。字母可以全小写,也可以大写。允许额外的前导和尾随空间。标准漏洞是被禁止的。更短的代码更好。
in:
5
out:
OP
N Q
M R
L S
K T
J U
I V
H W
G X
F Y
E Z
D A
C B
B C
A D
in:
1
out:
AB发布于 2018-02-28 11:27:21
,G:tPY"tf1Y2y@?tn+P])Z?]Pv1X!, % Do twice
G: % Push [1 2 ... n], where n is the input
tP % Duplicate, flip: pushes [n n-1 ... 1]
Y" % Run-length decoding: gives vector with n ones, n-1 twos ... (*)
tf % Duplicate, find: gives [1 2 3 ... n*(n-1)/2] (**)
1Y2 % Push string 'ABC...Z'
y % Duplicate from below: pushes [1 2 3 ... n*(n-1)/2] again
@? % If we are in the second iteration
tn % Duplicate, length: pushes n*(n-1)/2
+ % Add: gives [n*(n-1)/2+1 n*(n-1)/2+2 ... n*(n-1)*2]
P % Flip: gives [n*(n-1)/2 n*(n-1)/2-1 ... n*(n-1)/2+1]
] % End if
) % Index (1-based, modular) into the string. Gives a substring
% with the letters of one half of the parabola (***)
Z? % Sparse: creates a char matrix with the substring (***) written
% at specified row (*) and column (**) positions. The remaining
% positions contain char(0), which will be displayed as space
] % End do twice. We now have the two halves of the parabola, but
% oriented horizontally instead of vertically
P % Flip the second half of the parabola vertically, so that the
% vertex matches in the two halves
v % Concatenate the two halves vertically
1X! % Rotate 90 degrees, so that the parabola is oriented vertically.
% Implicitly display发布于 2018-02-28 13:33:25
i,j,k,l,m,h,o;f(n){char L[o=n*n][n*3];for(i=o;i--;)for(L[i][j=n*2]=h=k=0;j--;)L[i][j]=32;for(m=n;!h|~i;m-=1-h*2)for(h+(l=m)?++j:++h;l--;)L[h?i--:++i][j]=65+k++%26;for(;o--;)puts(L+o);}i, j, k, l, m, h, o;
f(n)
{
char L[o=n*n][n*3];
for (i=o; i--;)
for (L[i][j=n*2]=h=k=0; j--;)
L[i][j] = 32;
for (m=n; !h|~i; m-=1-h*2)
for (h+(l=m)?++j:++h; l--;)
L[h?i--:++i][j] = 65 + k++%26;
for (; o--;)
puts(L+o);
}发布于 2018-02-28 13:36:31
n->{for(int l=n*++n/2,r=l,i=1,j=0;l>0;j=j-->0?j:i++)System.out.printf("%"+(n-i)+"c%"+(2*i-1)+"c%n",--l%26+65,r++%26+65);}n->{ // int-accepting consumer
for( // loop
int l=n*++n/2, // declare l (left) is the first character to print.
// Oh, and n is increased to reduce byte count later.
r=l, // r (right) is the second character to print.
i=1, // i is the "outer-loop" index
j=0; // j is the "inner-loop" index
l>0; // while there are characters to print
j=j-->0?j:i++) // simulate two loops in one,
// where j starts from 0 and always decreases until it reaches 0
// at which point j is reset to i and i is increased
System.out.printf( // Print...
"%"+(n-i)+"c%"+(2*i-1)+"c%n", // 2 characters
// - the first with n-i-1 whitespaces (remember, n was increased)
// - the second characters with 2*i-2 whitespaces
--l%26+65, // the first character to print is the left one, we decrease it.
r++%26+65 // the second character to print is the right one, we increase it.
); //
// end loop
} // end consumerhttps://codegolf.stackexchange.com/questions/156855
复制相似问题