1

最近、難解なプログラミング言語に関する質問に遭遇しました。その言語のツールがあります。

> - increases the data pointer (so it points to the next cell in the array);
< - decreases the data pointer;
+ - increments the value in the current memory cell (i.e. one pointed by data pointer);
- - decrements the value in the current memory cell;
. - takes the value of current memory cell, converts it to character and prints to output;
, - takes one character from input and puts its ASCII code to the current memory cell.


[ - peeks at current cell, if the value here is non-zero, then simply proceeds to the next instruction, but if the value is 0, then searches forward for corresponding ] and sets code pointer immediately after this (i.e. skips the block within brackets);
] - moves code pointer back, to corresponding [.

: - takes the value of current memory cell and prints to output as a decimal integer;
; - takes next decimal integer (probably, consisting of several digits) from input and puts it to the current cell.

そこで、この言語で、2 つの数値 a と b を入力し、それらを cell0 と cell1 に入れ、この 2 つの数値の合計を出力するプログラムを作成する必要があります。追加の要件があります(私が問題を抱えていました)が、プロセスの後に3つのセルが必要であり、セル0はaを保持し、セル1はbを保持し、セル2はa + bを保持します。

これが私の分析です。セル3に合計を入れて印刷する方法を見つけるのは簡単だと思い;>;<[->>+]>[->+]>:ました。ただし、この方法では、処理後、cell0 と cell1 はすべて a と b ではなく 0 を保持します。だから私はそれを達成するために上記のツールを使用する方法を見つけようとしていました.ツールを考えると、それはバッテリーのようなものです.1つのバッテリーから別のバッテリーにエネルギーを移動することしかできませんが、あるバッテリーから別のバッテリーにエネルギーをコピーすることはできません. . もしそうなら、セル0とセル1を保存しようとしている間、合計を取得することはできません.

私の質問の下にある@ user3386109コメントの情報に感謝します。「エネルギーバランス」をごまかす方法があることに気づきました。ループ内で 2 つ以上のセルをインクリメントできます。したがって、5 つのセルを使用し、最初のセルと 2 番目のセルの a と b を合計演算を実行しながら 4 番目と 5 番目のセルに転送します。したがって、私のアルゴリズムは次のようになります。

    ;>; input two numbers a and b
    <[->>+>+] if the first cell is not zero then we keep decrementing it and incrementing the number in 3rd cell and 4th cell until it's zero.
    >[->+>>+] if the second cell is not zero then we keep decrementing it and incrementing the number in 3rd cell and 5th cell until it's zero.

    then we transfer back value a and b from 4th and 5th cell to 1st and 2nd cell
    >>[-<<<+]>[-<<<+]
    <<: go back 3rd cell and print out.

最後に私のコードは次のとおりです。

;>;<[->>+>+]>[->+>>+]>>[-<<<+]>[-<<<+]<<:

しかし、そうではありません。何度か確認しましたが、バグを見つけることができませんでした。誰か助けて?どうも!!!

4

1 に答える 1

2

使用している言語は、基本的にはBrainfuck;です (便宜上and演算子を追加し:ていますが、言語の動作には実際には影響しません)。

user3386109 がコメントで指摘したように、秘訣は入力を結果に追加しながらコピーすることです。そのための解決策はほぼ正しいですが、各ループ内でメモリポインターを元に戻すのを忘れているようです。ループは、反復ごとにさらに右に移動するだけです。ループ中にテープに沿って移動するような楽しいことをしている場合を除き、通常はループごとに と の<バランス>をとります。

問題の解決策の 1 つを次に示します。

;              Read a into cell 0
[->+>>+<<<]    Add a to cells 1 and 3
;              Read b into cell 0
[->>+>+<<<]    Add b to cells 2 and 3
>>>:           Print cell 3 (with cells 1 and 2 holding copies of a and b)

ここでこれを(一種の)テストできます。;これは標準の Brainfuck インタープリターなので、 と をサポートしていません:が、文字!(33) とA(65)を追加するとb(98) になります。

Brainfuck の問題を解決する方法の詳細については、上部にリンクした esolangs の記事と、その下部にある多くのリンクをお勧めします。

于 2016-10-14T14:58:31.547 に答える