Const n=20; var a,b:array[1..n] of integer; i,j,k,m,c:integer; begin Randomize; writeln('Исходный массив:'); for i:=1 to n do begin a[i]:=random(51)-25; write(a[i]:5); end; writeln; j:=0; for i:=1 to n do if a[i]<0 then begin j:=j+1; b[j]:=a[i]; end; m:=j; for k := 1 to m-1 do for i := 1 to m-k do if (b[i]<b[i+1]) then begin c:=b[i]; b[i]:=b[i+1]; b[i+1]:=c; end; writeln('Вс массив:'); for i:=1 to m do write(b[i]:5); writeln; j:=0; for i:=1 to n do if a[i]<0 then begin j:=j+1; a[i]:=b[j]; end; writeln('Полученный массив:'); for i:=1 to n do write(a[i]:5); writeln; end.
#include <iostream>
int main() {
int arr[100], n, first = 0, second = 0;
std::cin >> n;
for (int i = 0; i < n; ++i)
std::cin >> arr[i];
for (int i = 1; i < n; ++i) {
const int diff = arr[i] - arr[i - 1];
if (diff > 0)
if (arr[i] <= 2000)
first += diff;
else if (arr[i - 1] >= 2000)
second += diff;
else {
first += 2000 - arr[i - 1];
second += arr[i] - 2000;
}
}
std::cout << first << ' ' << second;
return 0;
Объяснение:
язык python
var
a,b:array[1..n] of integer;
i,j,k,m,c:integer;
begin
Randomize;
writeln('Исходный массив:');
for i:=1 to n do
begin
a[i]:=random(51)-25;
write(a[i]:5);
end;
writeln;
j:=0;
for i:=1 to n do
if a[i]<0 then begin j:=j+1; b[j]:=a[i]; end;
m:=j;
for k := 1 to m-1 do
for i := 1 to m-k do
if (b[i]<b[i+1]) then
begin
c:=b[i]; b[i]:=b[i+1]; b[i+1]:=c;
end;
writeln('Вс массив:');
for i:=1 to m do write(b[i]:5);
writeln;
j:=0;
for i:=1 to n do
if a[i]<0 then begin j:=j+1; a[i]:=b[j]; end;
writeln('Полученный массив:');
for i:=1 to n do write(a[i]:5);
writeln;
end.
Пример:
Исходный массив:
-15 -8 -6 -13 15 24 5 -2 14 -1 19 -2 -7 -8 -23 20 -2 7 -2 -10
Вс массив:
-1 -2 -2 -2 -2 -6 -7 -8 -8 -10 -13 -15 -23
Полученный массив:
-1 -2 -2 -2 15 24 5 -2 14 -6 19 -7 -8 -8 -10 20 -13 7 -15 -23