24h購物| | PChome| 登入
2012-09-13 09:59:10| 人氣1,464| 回應0 | 上一篇 | 下一篇

[UVA][模擬] 11956 - Brainfuck

推薦 0 收藏 0 轉貼0 訂閱站台

F  — Brainfuck

Time Limit: 3 sec
Memory Limit: 32 MB

Recently your friend Bob has bought a new brainfuck programmable LED display. However executing a program directly on LED display takes huge amount of time. Because of this, now Bob decided to write the interpreter of display instruction set in order to test and debug all his programs on his PC and only after that execute his code on LED display. However Bob knows only one programming language — its certainly brainfuck (otherwise he would not have bought this LED display). So he asks you to write him an interpreter.

A display’s program is a sequence of commands executed sequentially. The commands of the display processor is a subset of brainfuck language commands. The commands that processor was capable to execute were ’>’, ’<’, ’+’, ’–’ and ’.’, which are described in the table below. Moreover, this LED display has an array of 100 bytes of circular memory (initialised with zeros) and a pointer to this array (initialised to point to the leftmost byte of the array). This means, that after incrementing a pointer, which points to the rightmost byte of memory, it will point to the leftmost byte and vice versa. Individual bytes are circular as well, so increasing a 255 gives a 0 and vice versa.

CommandDescription
>Increment the pointer (to point to the next cell to the right).
<Decrement the pointer (to point to the next cell to the left).
+Increment (increase by one) the byte at the pointer.
Decrement (decrease by one) the byte at the pointer.
.Output the value of the byte at the pointer.

INPUT

There is a number of tests T (T ≤ 100) on the first line. After T tests follow. Each test case is a sequence of display processor commands on a separate line. You can assume that line length is less than 100000.

OUTPUT

For each test output a single line "Case T: D". Where T is the test number (starting from 1) and D is display’s memory dump in hexademical numeration system after executing given brainfuck program. Every byte must be separated exactly by one space character. See examples for clarification. Please note, that the sample input and output is divided into several lines only for convenience.

SAMPLE INPUT

1
..++<><<+++>>++++++++++++++++++++++++++>>>+++<+...++<><<+++>>++++++++++++++++++++++++++>>>+++<+...++<><<+++>>++++++++++++++++++++++++++>>>+++<+.

SAMPLE OUTPUT

Case 1: 1F 00 20 03 1D 03 01 03 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 03 00

題目名稱太吸引人了


#include <stdio.h>

int main() {
int t, cases = 0;
char cmd[100005];
scanf("%d", &t);
getchar();
while(t--) {
gets(cmd);
unsigned char mem[100] = {};
int idx = 0, i;
for(i = 0; cmd[i]; i++) {
switch(cmd[i]) {
case '>':idx++;break;
case '<':idx--;break;
case '+':mem[idx]++;break;
case '-':mem[idx]--;break;
}
if(idx >= 100) idx = 0;
if(idx < 0) idx = 99;
}
printf("Case %d:", ++cases);
for(i = 0; i < 100; i++)
printf(" %02X", mem[i]);
puts("");
}
return 0;
}
 

台長: Morris
人氣(1,464) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA] 12060 - All Integer Average
此分類上一篇:[UVA][dp] 11578 - Situp Benches

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文