0

我正在开发一个显示随机数的图灵应用程序,然后显示该数字滚动了多少次,然后找到模式,随机滚动次数的最大值,并显示哪个数字(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 "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"
4

2 回答 2

1

人们仍在使用图灵,这真是太好了。这是最好的初学者语言。

您当前的代码捕获最大频率。您还希望捕获最大频率值的索引。仔细查看您的代码,以确定您需要的索引值可以或正在计算的位置。声明一个变量来捕获它,比如highestIndex. 然后添加代码来进行捕获和打印。我认为这将需要3行。这些是什么?他们去哪里?当您了解当前程序的工作原理时,您会看到...

于 2013-05-30T14:36:44.033 回答
1

你需要一个额外的变量来保存你发现最高的骰子:

var highestNum : int
var highestIndex : int

highestNum := 0
highestIndex := 0

. . .

    highestNum := freq (counter)
    highestIndex := counter
于 2013-05-30T14:39:10.370 回答