Input contains many lines(at least two), each line contains 1 integer number(bigger than 0) and the last line is a single '%'. Print the second large number, according to the following example.
Samples Input:
10
9
24
21
6
12
%
Samples Output:
SECOND LARGE NUMBER = 21

解决方案 »

  1.   

    涉及2个主要知识点:1、控制台程序输入输出;2、排序算法uses
      SysUtils;//冒泡排序
    procedure BubbleSort(var ary: array of integer);
    var
      i,j,intTmp:integer;
    begin
      for i:=0 to high(ary) do
      begin
        for j:=0 to high(ary)-1 do
        begin
          if ary[j]>ary[j+1] then
          begin
            intTmp:=ary[j];
            ary[j]:=ary[j+1];
            ary[j+1]:=intTmp;
          end;
        end;
      end;
    end;var
      input: string;
      num,i,len: integer;
      ary: array of integer;
    begin
      len := 0;
      SetLength(ary,len);
      //to get integer data array from keyboard, end with '%'
      repeat
        readln(input);
        num := StrToIntDef(input,0);
        if (num>0) then
        begin
          inc(len);
          SetLength(ary,len);
          ary[len-1] := num;
        end;
      until input='%';
      if len<2 then
      begin
        writeln('invalid input lines');
        exit;
      end;  //排序
      BubbleSort(ary);
      //输出倒数第二个数
      writeln(IntToStr(ary[len-2]));
      readln(input);
    end.
      

  2.   

    不需要排序:{$APPTYPE CONSOLE}
    program test165;var
      m, n, x: integer;{ main }
    begin
      m := 0; n := 0;
      repeat
        {$I-} readln(x); {$I+}
        if IOResult <> 0 then break;
        if x > m then
        begin
          n := m;
          m := x;
        end
        else
          if x > n then n := x;      
      until false;
      writeln(n);
    end.