当前你的浏览器版本过低,网站已在兼容模式下运行,兼容模式仅提供最小功能支持,网站样式可能显示不正常。
请尽快升级浏览器以体验网站在线编辑、在线运行等功能。

建议使用的浏览器:

谷歌Chrome 火狐Firefox Opera浏览器 微软Edge浏览器 QQ浏览器 360浏览器 傲游浏览器

2849:brainf*ck

题目描述

brainf*ck is the ungodly creation of Urban Mller, whose goal was apparently to create a Turing-complete language for which he could write the smallest compiler ever. http://en.wikipedia.org defines it as “a computer programming language designed to challenge and amuse programmers, and is not suitable for practical use. Its name has been variously euphemized, as in brainf*ck.”

A brainf*ck program has an implicit byte pointer, called “the pointer”, which is free to move around within an array of 32768 bytes, initially all set to zero. The pointer itself is initialized to point to the beginning of this array.

The brainf*ck programming language consists of seven commands, each of which is represented as a single character. Note: “Industry standard” brainf*ck actually has eight commands, but for the purposes of this problem one command was intentionally omitted.


COMMAND OPERATION
> Increment the pointer.
Incrementing a pointer value of 32767
results in a pointer value of 0.
< Decrement the pointer.
Decrementing a pointer value of 0
results in a pointer value of 32767.
+ Increment the byte at the pointer.
Incrementing the byte value 255 results
in the byte value 0.
- Decrement the byte at the pointer.
Decrementing the byte value 0 results
in the byte value 255.
. Output the character whose ASCII
value is the byte at the pointer
[ Jump forward past the matching ] if the
byte at the pointer is zero.
] Jump backward to the matching [
unless the byte at the pointer is zero.


For this problem, you will write a program that reads in, parses and executes a brainf*ck program.

输入解释

The first line of input contains an integer N, (1 N 100) , which is the number of brainf*ck programs that follow. Each program consists of one or more lines of brainf*ck commands ending with a line that consists of the word `end'. Your program should ignore any illegal characters (I.E. any character not in the set: <>+-.[]), If a percent sign (%) is encountered during parsing, the remainder of the line should be discarded. This constitutes a comment. The maximum number of commands in a brainf*ck program is 128000.

输出解释

For each brainf*ck program, your program should output the text ‘PROGRAM #n:’ on a single line (where n is the program number: 1 ≤ nN), followed by the output generated by the brainf*ck program, followed by a single newline character. The only possible parsing error that can occur is if there is an unmatched [ or ] in the brainf*ck program. If your program encounters such an error, it should simply print ‘COMPILE ERROR’ instead of executing the program. All brainf*ck programs will use no more than the specified 32768 bytes of memory.

输入样例
3
++++++++[>+++++++++ % hello-world.
<-]>.<+++++[>++++++<-]>-.+++++++..
+++.<++++++++[>>++++<<-]>>.<<++++[>
------<-]>.<++++[>++++++<-]>.+++.
------.--------.>+.
end
+++[>+++++++[.
end
%% Print alphabet, A-Z.
+ + + + + +++++++++++++++++++++>
++++++++++++++++++++++++++++++++
++++++++++++++++++++++++++++++++
+< [ >.+<- ]
end
输出样例
PROGRAM #1:
Hello World!
PROGRAM #2:
COMPILE ERROR
PROGRAM #3:
ABCDEFGHIJKLMNOPQRSTUVWXYZ

该题目是Virtual Judge题目,来自 北京大学POJ

题目来源 Greater New York 2005

源链接: POJ-2849

最后修改于 2020-10-29T06:45:51+00:00 由爬虫自动更新

共提交 0

通过率 --%
时间上限 内存上限
1000 65536