该程序说要读取包含未知数量的文件。每个数字都是数字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
....
我还好吗?
答案 0 :(得分: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