我正在开发一个显示随机数的图灵应用程序,然后显示该数字的滚动次数,然后查找模式,随机滚动次数的最高值,并显示哪个数字(1-6)滚动最多。谢谢你的帮助。
var numberDice : int
var freq : array 1 .. 6 of int
var highestNum : int
highestNum := 0
for j : 1 .. 6
freq (j) := 0
end for
for i : 1 .. 25
randint (numberDice, 1, 6)
freq (numberDice) := freq (numberDice) + 1
end for
for counter : 1 .. 6
if freq (counter) > highestNum then
highestNum := freq (counter)
end if
end for
put "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
put "Number Frequency"
put "1 ", freq (1)
put "2 ", freq (2)
put "3 ", freq (3)
put "4 ", freq (4)
put "5 ", freq (5)
put "6 ", freq (6)
put " "
put "Mode: ", highestNum
put "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
答案 0 :(得分:1)
人们仍在使用图灵,这很棒。这是最好的初学者的语言。
您当前的代码会捕获最大频率值。您还想捕获最大频率值的索引。仔细查看您的代码,以确定您需要或正在计算所需索引值的位置。声明一个变量来捕获它,比如highestIndex
。然后添加代码进行捕获和打印。我认为需要3行。这些是什么?他们去哪里?当您了解当前程序的工作原理时,您将看到......
答案 1 :(得分:1)
你需要一个额外的变量来保存你发现最高的骰子:
var highestNum : int
var highestIndex : int
highestNum := 0
highestIndex := 0
. . .
highestNum := freq (counter)
highestIndex := counter