pascal

2014-01-11 8:51 am
type
mark = array [1..6] of integer ;
(* an array type of integers with 6 elements *)
var
t1mark, t2mark, t3mark, total : mark;
name : array[1..6] of string;
i,(* for loop counter *)
mintotal, min_ndx (* for finding the minimum value of totals *)
: integer;
begin
(* name table *)
name[1]:='John Chan';
name[2]:='Tim Tong';
name[3]:='Regina Lau';
name[4]:='Danny Tsang';
name[5]:='Roy Leung';
name[6]:='Jo Lee';

(* Input marks of the 3 tests for all 6 students *)
for i:=1 to 6 do
begin
writeln ('Enter the three test marks for', name[i], ':');
readln (t1mark[i], t2mark[i],t3mark[i]);
end;

(* Calculate the totals of the 3 tests for all 6 students *)
for i:=1 to 6 do
total[i]:= t1mark[i]+ t2mark[i] + t3mark[i] ;

Writeln;
Writeln('Students failed:');
(* Display names of those students with average mark less than 50 *)
for i:=1 to 6 do
if (total[i]/3)< 50 then
writeln (name[i]);
(* Find the student with the lowest total mark *)
for i := 1 to 6 do
if total[i]/3 < mintotal then
begin
mintotal := total[i];
min_ndx := i ;
end ;
writeln(name[min_ndx],' got the lowest average mark (',mintotal / 3:0:1,')');
readln
end.
更新1:

http://www.uwants.com/viewthread.php?tid=16947809&extra=page%3D2&frombbs=1

回答 (1)

2014-01-13 2:32 am
✔ 最佳答案
找最低分的邏輯錯了,應先假設第一位係最低,然後由2..6找有沒有人比他更低。

// assume first student has the lowest score
mintotal := total[1];
min_ndx := 1 ;

// loop through all others and see if there is another one lower
for i := 2 to 6 do
if total[i] < mintotal then
begin
mintotal := total[i];
min_ndx := i ;
end ;
writeln(name[min_ndx],' got the lowest average mark (',mintotal / 3:0:1,')');
readln
end.


收錄日期: 2021-04-26 23:57:02
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20140111000051KK00015

檢視 Wayback Machine 備份