From fbd4f2129ce8fe106391302896dd86e05b2f331b Mon Sep 17 00:00:00 2001 From: nsfisis Date: Fri, 5 Dec 2025 04:08:22 +0900 Subject: add files --- archive/q/brainf-ck/index.html | 227 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 227 insertions(+) create mode 100644 archive/q/brainf-ck/index.html (limited to 'archive/q/brainf-ck/index.html') diff --git a/archive/q/brainf-ck/index.html b/archive/q/brainf-ck/index.html new file mode 100644 index 0000000..622f1b2 --- /dev/null +++ b/archive/q/brainf-ck/index.html @@ -0,0 +1,227 @@ + + + + + 問題 #3 | Albatross.PHP + + + + + +
+ +
+
+

問題 #3

+ + +

Brainf*ck

+

+ Brainf*ck を実装してください。標準入力から Brainf*ck のソースコードが渡されます。Brainf*ck の仕様のうち、入力命令である「,」は未実装で構いません。 + +一部明確に挙動が定められていない部分については、以下の仕様とします。 +メモリの各セルは 8 bit の符号なし整数です。今回の入力でオーバーフローは発生しません。 +メモリのアドレスは非負整数です。今回の入力で負のアドレスへポインタを動かすことはありません。 +Brainf*ck に存在する命令以外の文字を受け取った場合は無視してください。 +今回の入力に不正なプログラムは含まれません。すなわち、「[」と「]」は必ず釣り合っています。 +

+

実装例

+
$source = stream_get_contents(STDIN);
+$pc = 0;
+$memory = [];
+$ptr = 0;
+
+while ($pc < strlen($source)) {
+  switch ($source[$pc]) {
+  case '>':
+    $ptr++;
+    break;
+  case '<':
+    $ptr--;
+    break;
+  case '+':
+    if (!isset($memory[$ptr])) {
+      $memory[$ptr] = 0;
+    }
+    $memory[$ptr]++;
+    break;
+  case '-':
+    if (!isset($memory[$ptr])) {
+      $memory[$ptr] = 0;
+    }
+    $memory[$ptr]--;
+    break;
+  case '.':
+    echo chr($memory[$ptr]);
+    break;
+  case '[':
+    if (!isset($memory[$ptr]) || $memory[$ptr] === 0) {
+      $depth = 1;
+      while ($depth > 0) {
+        $pc++;
+        if ($source[$pc] === '[') {
+          $depth++;
+        } elseif ($source[$pc] === ']') {
+          $depth--;
+        }
+      }
+    }
+    break;
+  case ']':
+    if (isset($memory[$ptr]) && $memory[$ptr] !== 0) {
+      $depth = 1;
+      while ($depth > 0) {
+        $pc--;
+        if ($source[$pc] === ']') {
+          $depth++;
+        } elseif ($source[$pc] === '[') {
+          $depth--;
+        }
+      }
+    }
+    break;
+  }
+  $pc++;
+}
+

ランキング

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
ランクID作者サイズ投稿日時
1 + #101 + takaram263 byte2024-03-09 14:33:37
2 + #107 + m3m0r7273 byte2024-03-09 14:50:06
3 + #7 + nsfisis (staff)341 byte2024-03-07 16:02:44
4 + #38 + tadsan (staff)343 byte2024-03-08 05:43:06
5 + #109 + hanhan1978392 byte2024-03-09 15:33:46
6 + #17 + yamamoto-hiroya415 byte2024-03-08 02:05:50
7 + #42 + rinchoku481 byte2024-03-08 07:55:27
8 + #23 + kunikiya495 byte2024-03-08 02:48:15
9 + #2 + blue-goheimochi (staff)1043 byte2024-03-06 08:26:28
10 + #3 + muno92 (staff)1043 byte2024-03-06 08:42:52
11 + #62 + okashoi1043 byte2024-03-08 16:45:23
12 + #77 + masnmt1043 byte2024-03-09 04:52:52
+
+ + +
+

+ すべての回答を見る +

+
+ + + -- cgit v1.2.3-70-g09d2