番号スパイラルは、左上の正方形の番号が1の無限グリッドです。スパイラルの最初の5つの層は次のとおりです。
あなたの仕事は、行yと列xの数を見つけることです。
例:
Input: 2 3
Out : 8
Input: 1 1
Out : 1
Input: 4 2
Out : 15
注意:
- 任意のプログラミング言語が許可されています。
- これはコードゴルフの挑戦なので、最短のコードが勝ちます。
- 幸運を祈る!
番号スパイラルは、左上の正方形の番号が1の無限グリッドです。スパイラルの最初の5つの層は次のとおりです。
あなたの仕事は、行yと列xの数を見つけることです。
例:
Input: 2 3
Out : 8
Input: 1 1
Out : 1
Input: 4 2
Out : 15
注意:
回答:
f(x,y,z){z=x>y?x:y;z=z*z-~(z%2?y-x:x-y)-z;}
スパイラルにはいくつかの「腕」があります。
12345
22345
33345
44445
55555
位置はarm max (x 、y )(変数に割り当てられます)にあります。次に、アームnの最大数はn 2で、アームの左下と右上の位置を交互に切り替えます。減算のxからyは配列与える- N + 1 、- N + 2 、... 、- 1 、0 、1 、... 、n個z
アームに沿って移動 N我々はパリティに基づいて適切な符号を選択して、 nは、により調整 N - 1 0から始まるシーケンスを取得するために、およびからこの値を減算 N 2。
バイトを保存してくれたXcoder氏に感謝します。
f(x,y,z){z=x>y?x:y;z=z*z-~(z%2?x-y:y-x)-z;}
1バイト節約します。
f(1, 1)
、値を返します1
。フッター部5を介して、X = 1~5及びy = 1をループし、機能が問題に示すすべての入力のために正しいことを実証するために、そのようなすべての値の関数を呼び出し、グリッドにその出力を印刷します。
def f(a,b):M=max(a,b);return(a-b)*(-1)**M+M*M-M+1
@ChasBrownのおかげで-4バイト
@Shaggyのおかげで-1バイト
初めてのゴルフ!これは最適ではありませんが、何であれ知っています。
基本的に、@ Doorknob Cコードと同じ原理で実行されます。
M**2
に置き換えることができると確信していM*M
ます。
X>ttq*QwoEqGd*+
オンラインでお試しください!
マトリックスとして収集して印刷する
編集:@Doorknobの答えと同じテクニックですが、着いたところが異なります。
X> % Get the maximum of the input coordinates, say n
ttq* % Duplicate that and multiply by n-1
Q % Add 1 to that. This is the diagonal value v at layer n
wo % Bring the original n on top and check if it's odd (1 or 0)
Eq % Change 1 or 0 to 1 or -1
Gd % Push input (x, y) again, get y - x
* % Multiply by 1 or -1
% For odd layers, no change. For even layers, y-x becomes x-y
+ % Add that to the diagonal value v
% Implicit output
代替21バイトソリューション:
Pdt|Gs+ttqq*4/QJb^b*+
オンラインでお試しください!
行列として収集して印刷
する上記から、必要な関数は
いくつかの基本的な計算では、最大2つの数値の1つの式が
これは、ソリューションが実装する機能です。
Doorknobのソリューションからいくつかのビールに適応しました。
wV
nU²ÒNr"n-"gUv
:Implicit input of integers U=x and V=y
wV :Maximum of U & V
\n :Reassign to U
U² :U squared
Ò :-~
"n-" :Literal string
Uv :Is U divisible by 2? Return 0 or 1
g :Get the character in the string at that index
Nr :Reduce the array of inputs by that, where n is inverse subtraction (XnY = Y-X)
n :Subtract U from the result of the above
A~Qh.MZQh-+*-GH^_1Q*
Rushabh Mehta の答えのほぼ文字通りの翻訳。
説明:A~Qh.MZQh-+*-GH^_1Q* | Full code
A~Qh.MZQh-+*-GH^_1Q*QQQ | Code with implicit variables filled
| Assign Q as the evaluated input (implicit)
A | Assign [G,H] as
~Q | Q, then assign Q as
h.MZQ | Q's maximal value.
| Print (implicit)
h-+*-GH^_1Q*QQQ | (G-H)*(-1)^Q+Q*Q-Q+1
»Ḃ-*×_‘+»²_»ʋ
((({}<>))<>[(({}))]<{({}[()])<>}>)<>{}((){({}[()])({})<><([{}])><>}{}<>{}<>)
ZÐ<*>ŠGR}¥+
@Emignaがに変更さÈi
れたため、-1バイトG
。
@sundarのMATL回答のポートなので、必ず彼に賛成してください!
オンラインそれを試してみたり、すべてのテストケースを確認してください。
説明:
Z # Get the maximum of the (implicit) input-coordinate
# i.e. [4,5] → 5
Ð # Triplicate this maximum
< # Decrease it by 1
# i.e. 5 - 1 → 4
* # Multiply it
# i.e. 5 * 4 → 20
> # Increase it by 1
# i.e. 20 + 1 → 21
Š # Triple swap the top threes values on the stack (a,b,c to c,a,b)
# i.e. [4,5], 5, 21 → 21, [4,5], 5
G } # Loop n amount of times
R # Reverse the input-coordinate each iteration
# i.e. 5 and [4,5] → [5,4]→[4,5]→[5,4]→[4,5] → [5,4]
¥ # Calculate the delta of the coordinate
# [5,4] → [1]
+ # And add it to the earlier calculate value (output the result implicitly)
# 21 + [1] → [22]
Èi
可能性がありますG
。
x = {5, 8};
そう:
m = Max[x];
Subtract @@ x (-1)^m + m^2 - m + 1
(*
54
*)