2014-03-26 33 views
-1

我想在我的TI计算器上编程一个程序,该程序使用一种通过算法(不通过对列表进行排序)查找序列中第二大整数。 任何人都可以帮助TI计算器程序或一般简单的程序。Second-Largest Integer

回答

1

算法很简单:

# Start by getting the first two numbers (in order). 

if num[1] > num[2]: 
    set first to num[1] 
    set second to num[2] 
else: 
    set first to num[2] 
    set second to num[1] 

# Process every other number. 

for each index 3 through size(num) inclusive: 
    # If greater than current highest, insert at top. 

    if num[index] > first: 
     second = first 
     first = num[index] 
    else: 
     # Otherwise if greater than current second highest, insert there. 

     if num[index] > second: 
      second = num[index] 

它基本上保持了两个最高编号列表并将其替换,根据需要,比较所有其它的数字时。这是一个按要求的单程算法。

您可能还想考虑在列表中存在重复项时您想要的行为。例如,列表1 1 2目前会给你1作为列表中第二高的整数。如果这是不可接受的,那么就必须对算法进行小的调整。

无论如何,我在那里给你的是一个很好的起点,并且将它翻译成你的TI计算器语言是一个我将留给你的任务。