Const n = 2; m = 5; var a: array [1..n, 1..m] of integer; i, j, x: integer; begin writeln('Исходная матрица: '); for i := 1 to n do begin for j := 1 to m do begin a[i, j] := random(15) + 1; write(a[i, j]:4); end; writeln; end; for j := 1 to m do begin x := a[1, j]; a[1, j] := a[2, j]; a[2, j] := x; end; writeln; writeln('Итоговая матрица: '); for i := 1 to n do begin for j := 1 to m do write(a[i, j]:4); writeln; end; end.
// PascalABC.NET 3.1, сборка 1204 от 24.03.2016 begin var F,G:file of integer; Assign(F,'FFile.dat'); Rewrite(F); for var i:=1 to 30 do Write(F,Random(-20,20)); // Файл F создан и заполнен Assign(G,'GFile.dat'); Rewrite(G); F.Seek(0); var e:integer; Print('F:'); while not F.Eof do begin Read(F,e); Print(e); if e>0 then Write(G,e) end; F.Seek(0); while not F.Eof do begin Read(F,e); if e<0 then Write(G,e) end; F.Seek(0); while not F.Eof do begin Read(F,e); if e=0 then Write(G,e) end; F.Close; Writeln; Print('G:'); G.Seek(0); while not G.Eof do begin Read(G,e); Print(e) end; G.Close end.
Данное решение неоптимально по времени, поскольку файл читается трижды. Можно сделать иной вариант, читая данные за один проход и помещая в две вс структуры памяти отрицательные и нулевые элементы.
// PascalABC.NET 3.1, сборка 1204 от 24.03.2016 begin var F,G:file of integer; Assign(F,'FFile.dat'); Rewrite(F); for var i:=1 to 30 do Write(F,Random(-20,20)); // Файл F создан и заполнен Assign(G,'GFile.dat'); Rewrite(G); var n:=F.FileSize; var neg,zer:array of integer; SetLength(neg,n); SetLength(zer,n); F.Seek(0); var e:integer; var ineg:=0; var izer:=0; Print('F:'); while not F.Eof do begin Read(F,e); Print(e); if e>0 then Write(G,e) else if e<0 then begin neg[ineg]:=e; Inc(ineg) end else begin zer[izer]:=e; Inc(izer) end end; F.Close; Writeln; SetLength(neg,ineg); foreach e in neg do Write(G,e); SetLength(zer,izer); foreach e in zer do Write(G,e); Print('G:'); G.Seek(0); while not G.Eof do begin Read(G,e); Print(e) end; G.Close end.
Суть программы Input (A[255], input.txt); // Вводим из файла строку в массив А[255] N = 0; NMax = 0; // Счетчик удачных дней M = 0; // Счетчик длины массива (он может быть и меньше 255) Цикл по i от 1 до 255 If (A[i] <> "У") and (A[i] <> "Н") then M = i - 1; // Как только мы встретили значение, не равное ни У ни Н, так считаем это концом. Конец цикла по i If A[1] = "У" then N = 1 // Проверяем 1-ую ячейку Цикл по i от 1 до M-1 if (A[i] = "Н") and (A[i+1] = "У") then N = 1; // Начало серии У if (A[i] = "У") and (A[i+1] = "У") then N = N + 1; // Продолжение серии У if (A[i] = "У") and (A[i+1] = "Н") then // Окончание серии У if N >= NMax then NMax = N; // Проверяем длину серии end if Конец Цикла по i Output (output.txt, NMax) // вывод максимальной длины серии в файл. Конец программы
n = 2; m = 5;
var
a: array [1..n, 1..m] of integer;
i, j, x: integer;
begin
writeln('Исходная матрица: ');
for i := 1 to n do
begin
for j := 1 to m do
begin
a[i, j] := random(15) + 1;
write(a[i, j]:4);
end;
writeln;
end;
for j := 1 to m do
begin
x := a[1, j];
a[1, j] := a[2, j];
a[2, j] := x;
end;
writeln;
writeln('Итоговая матрица: ');
for i := 1 to n do
begin
for j := 1 to m do
write(a[i, j]:4);
writeln;
end;
end.