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
uj5u.com熱心網友回復:
求指教=求代碼?uj5u.com熱心網友回復:
涉及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.
uj5u.com熱心網友回復:
不需要排序:
{$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.
uj5u.com熱心網友回復:
uj5u.com熱心網友回復:
給翻譯了下,輸入多行整形資料,最后加上一行%,然后輸出結果為比較這些資料后,取第二大的那個數轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/19221.html
標籤:語言基礎/算法/系統設計
