最近、難解なプログラミング言語に関する質問に遭遇しました。その言語のツールがあります。
> - 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.
最後に私のコードは次のとおりです。
;>;<[->>+>+]>[->+>>+]>>[-<<<+]>[-<<<+]<<:
しかし、そうではありません。何度か確認しましたが、バグを見つけることができませんでした。誰か助けて?どうも!!!