如何使用Brainfuck中的循环来打印1到10之间的数字?可能吗?
我正在寻找解决此问题的方法。
答案 0 :(得分:3)
create table [dbo].SoTablename(
[FirstColumn] [nvarchar](10),
[SecondColumn] [nvarchar](10)
)
GO
insert into [dbo].SoTablename values ('Jack', 'apple')
insert into [dbo].SoTablename values ('Jack', 'banana')
insert into [dbo].SoTablename values ('Jack', 'orange')
insert into [dbo].SoTablename values ('Emma', 'apple')
insert into [dbo].SoTablename values ('Bob', 'banana')
Select * From SoTablename
;with temp as
(select
k.FirstColumn,
case
when exists (select * from dbo.SoTablename where FirstColumn = k.FirstColumn and SecondColumn = 'banana')
then 1
else
0
end as 'ExistsOrNot'
from
dbo.SoTablename k
)
Insert Into dbo.SoTablename
select
FirstColumn, 'banana'
from temp
where existsornot = 0
Select * From SoTablename
+++++++++++++++++++++++++++++++++++++++++++++++++ Cell 0 to '1'
>++++++++++ cell 1 to '\n'
>+++++++++ cell 2 to 9 as counter
[ Print numbers 1 to 9
<< Data pointer to cell 0
.+ Print and increment cell 0
>. Data pointer to cell 1 and print the newline
>- Data pointer to cell 2 and decrement counter
] Loop till counter is 0
+++++++++ Set cell 2 to 9
[ Set cell 0 to '1'
<<- Data pointer to cell 0 and decrement
>>- Data pointer to cell 2 and decrement counter
] Loop till counter is 0
<<. Data pointer to cell 0 and print '1'
-. Decrement cell 0 and print '0'
>. Data pointer to cell 1 and print newline
答案 1 :(得分:1)
TL; DR
-[>+<-----]>---<++++++++++<++++++++++[>>.+<.<-]>>---------.-.
END TL; DR
要在BrainF ** k中编程,就必须像每个程序(甚至是简单的程序)一样,从布局开始。
用于此的伪代码类似于:
Generate the character '0'
Move left and generate '\n'
Move left and generate the counter (10 numbers in this case)
Loop: Get back to the character '0', print it, increment it to '1', go to the newline, print it, go to the counter, and decrement it. End it when the counter is 0
Generate '1' and print it
Generate '0' and print it
但是,最后两个步骤可以简化为:
Go back to the digit '9'
Decrement it until '1' and print
Decrement it until '0' and print
这可以节省大量时间和字节个字符。
要生成字符“ 0”,请生成整数48(因为这是ASCII值)。为此,您可以转到Esolangs' BF Constants。查找数字48,我们发现-[>+<-----]>---
到目前为止,我们的程序是-[>+<-----]>---
以生成0
接下来,向左移动并生成\n
(换行符)。我们可以使用<++++++++++
。注意它是完全加号的。这是因为减少数字10的字符数没有太多空间。
到目前为止,我们的程序是-[>+<-----]>---<++++++++++
然后,向左移动并生成计数器。我们希望计数器为10以打印从0到9的数字。<++++++++++
。
到目前为止,我们的程序是-[>+<-----]>---<++++++++++<++++++++++
之后,开始循环[
。转到'0'>>
,将其打印.
,将其递增+
,转到换行符并打印<.
,转到计数器并将其递减,然后结束<-]
为零时循环。 [>>.+<.<-]
到目前为止,我们的程序是-[>+<-----]>---<++++++++++<++++++++++[>>.+<.<-]
最后,转到'9'>>
,将其递减直到为1并打印---------.
,然后将其递减直到为0并打印-.
。 ---------.-.
程序完成。
答案 2 :(得分:0)
++++++++++++++++++++++++++++++++++++++++++++++++ Let address 0 be the digit we want to print, starting with '0'
>++++++++++ Let address 1 be our newline character
>+++++++++ Let address 2 be our counter, starting at 9
[
- Decrement the counter
<<+. Increment the digit we want to print and print it
>. Print the newline
> Make sure we're at the counter again before we loop back
]
<< Move back to address 0
--------. Make address 0 '1'
-. Make address 0 '0'
答案 3 :(得分:0)
这是可能的。下面是代码:++++++++++>++++++++++[>+++++<-]>-.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>+.<<.>>--------.-.
可以更短一些,但是它仍然可以完成相同的任务。理论上,Brainf ***可以完成任何计算,因为它已经完成了Turing。这只是这些计算之一。