又一道free pascal题
来源:学生作业帮 编辑:神马作文网作业帮 分类:综合作业 时间:2024/11/16 15:06:00
又一道free pascal题
由M个数构成一个圆环,找出N个相邻的数,使其和为最大、最小.
样例输入与输出:
输入:
8 4
1 7 8 2 6 5 4 3
输出:
Max=23
Min=13
由M个数构成一个圆环,找出N个相邻的数,使其和为最大、最小.
样例输入与输出:
输入:
8 4
1 7 8 2 6 5 4 3
输出:
Max=23
Min=13
枚举 就行了;
(以下代码仅供参考)
program hh;
var
i,j,m,n,total,max,min:longint;
a:array[0..1000] of longint;
begin
readln(m,n);
for i:=1 to m do begin
read(a[i]);
a[m+i]:=a[i];
end;
min:=maxlongint;
max:=-maxlongint;
for i:=1 to m do begin
total:=0;
for j:=i to i+n-1 do
inc(total,a[j]);
if totalmax then max:=total;
end;
writeln('Max=',max);
writeln('Min=',min);
end.
(以下代码仅供参考)
program hh;
var
i,j,m,n,total,max,min:longint;
a:array[0..1000] of longint;
begin
readln(m,n);
for i:=1 to m do begin
read(a[i]);
a[m+i]:=a[i];
end;
min:=maxlongint;
max:=-maxlongint;
for i:=1 to m do begin
total:=0;
for j:=i to i+n-1 do
inc(total,a[j]);
if totalmax then max:=total;
end;
writeln('Max=',max);
writeln('Min=',min);
end.