Matrix55. Дана матрица размера M × N (M — четное число). Поменять местами верхнюю и нижнюю половины матрицы.
>Решение Pascal
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | program Matrix55; type matrix = array [1..10,1..10] of integer; var a:matrix; M, N, i, j:Integer; procedure Swap(var X,Y:Integer); begin if x<>y then begin X:=X+Y; Y:=X-Y; X:=X-Y; end; end; procedure SwapMatrixRow(var mat:matrix; X,Y:Integer); var j:integer; begin if x<>y then for j:=1 to N do swap(mat[x,j], mat[y,j]); end; begin Write('N: '); Readln(N); Write('M: '); Readln(M); for i:=1 to M do begin writeln(i,': '); for j:=1 to N do begin Write(j,' : '); Read(a[i,j]); end; end; for i:=1 to (M div 2) do SwapMatrixRow(a, i, (M div 2)+i); for i:=1 to M do begin for j:=1 to N do begin Write(' : ',a[i,j]); end; writeln(' : '); end; end. |
Решение C++
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | # include <iostream> # include <windows.h> # include <cmath> # include <iomanip> # include <fstream> # include "IOMatrix.h" using namespace std; int main () { SetConsoleCP(1251); SetConsoleOutputCP(1251); double matr[RMAX][CMAX]; int nr, nc; input (matr,"Matrix55.txt",nr,nc); if (nr%2==0){ output (matr,nr,nc); cout << endl; double temp; // временная переменная для хранения значения int half=nr/2; // определитель половины матрицы for (int col = 0; col < nc; ++col) { for (int row = 0; row < half; ++row) { temp = matr[row][col]; matr[row][col] = matr[row+half][col]; matr[row+half][col] = temp; } } cout << "Преобразованная матрица" << endl; output (matr,nr,nc); } else cout << "Введено не четное число строк!" << endl; system ("pause"); return 0; } |
IOMatrix.h
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | # include <iostream> # include <fstream> using namespace std; const int RMAX=100; const int CMAX=100; void input(double a[][CMAX],const char* name, int& nr, int& nc) { ifstream in (name); //cout << "Введите количество строк и столбцов матрицы: "; //cin >> nr >> nc; for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { in >> a[row][col]; } } in.close(); } void output(const double a[][CMAX],int nr, int nc) { for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { cout << '\t' << a[row][col] << " "; } cout << endl; } } void input(int a[][CMAX],const char* name, int& nr, int& nc) { ifstream in (name); //cout << "Введите количество строк и столбцов матрицы: "; //cin >> nr >> nc; for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { in >> a[row][col]; } } in.close(); } void output(const int a[][CMAX],int nr, int nc) { for (int row = 0; row < nr; ++row) { for (int col = 0; col < nc; ++col) { cout << '\t' << a[row][col] << " "; } cout << endl; } } |
Matrix55.txt
1 2 3 4 5 | 1 3 5 7 -3 0 3 1 3 2 7 14 15 16 17 13 5 5 33 0 1 |