0

该程序说要读取一个包含未知数量数字的文件。每个数字都是数字 1 到 9 之一。一个数字可以出现零次或多次,并且可以出现在文件中的任何位置。数字 0 结束数据。数据样本:

5 3 7 7 7 4 3 3 2 2 2 6 7 4 7 7 2 2 9 6 6 6 6 6 8 5 5 3 7 9 9 9 0

程序应该读取一次数据并打印在连续位置出现最多的数字和出现的次数。忽略打领带的可能性。

它应该打印:数字 6 出现 5 次。

到目前为止,这是我的粗略逻辑:

  scan for num
  while num is not zero
  store the num into num2 (another variable)
  scan for another variable
  compare and check if num is equal to num2
  if it is increment count variable (numCount)
  declare another count variable (numCount2) and initialize it to 0
  check if numCount > numCount2 and 
  store the value of previous count variable into numCount2
  ....

我过得好吗?

4

1 回答 1

1

我认为这很混乱,因为您正在扫描 while 循环的数字,然后又在循环内扫描。我会这样做:

max_num = 0
max_count = 0
previous_num = 0
previous_count = 0

while read into num (stops when zero)
  if num equals previous_num
    increment previous_count
  else
    if previous_count > max_count
      max_count = previous_count
      max_num = previous_num
    previous_num = num
    previous_count = 0
if previous_count > max_count
  max_count = previous_count
于 2013-07-15T23:58:02.903 回答