Advertisement
Guest User

Untitled

a guest
Feb 19th, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <iomanip>
  4.  
  5. using namespace std;
  6. void input(int matr[10][10], int &n, int &m) {
  7. cout << "Введите размеры матрицы." << endl;
  8. cin >> n >> m;
  9. cout << "Введите элементы матрицы." << endl;
  10. for (int i = 0; i < n; i++)
  11. for (int j = 0; j < m; j++) {
  12. cin >> matr[i][j];
  13. }
  14. }
  15.  
  16. void change(int matr[10][10], int n, int m) {
  17. int x1=-1,y1=-1,x2=-1,y2=-1,c, check1 = 1, check2 = 1;
  18. for (int i=0;i<n;++i)
  19. for (int j=0;j<m;++j)
  20. if (check1 && matr[i][j] % 2 == 0) {
  21. x1 = i;
  22. y1 = j;
  23. check1 = 0;
  24. }
  25. for (int i=n-1;i>=0;i--) {
  26. for (int j = m-1; j >= 0; j--) {
  27. if (check2 && matr[i][j] == 0) {
  28. x2 = i;
  29. y2 = j;
  30. check2 = 0;
  31. }
  32. }
  33. }
  34.  
  35.  
  36. matr[x2][y2] = matr[x1][y1];
  37. matr[x1][y1] = 0;
  38. }
  39.  
  40. void output(int matr[10][10], int n, int m){
  41. for (int i=0;i<n;i++){
  42. for (int j=0;j<m;j++){
  43. cout.width(5);
  44. cout << matr[i][j] << " "; }
  45. cout << endl;
  46. }
  47. }
  48.  
  49. int main(){
  50. int matr[10][10], n=0, m=0, p=0, z=0;
  51. setlocale(LC_ALL,".1251");
  52. input(matr,n,m);
  53. cout << "Исходная матрица:\n";
  54. for (int i=0;i<n;i++){
  55. for (int j=0;j<m;j++)
  56. cout << matr[i][j] << " ";
  57. cout << endl;
  58. }
  59. for (int i=0;i<n;i++)
  60. for (int j=0;j<m;j++) {
  61. if (matr[i][j] % 2 == 0)
  62. p++;
  63. if (matr[i][j] == 0) {
  64. z++;
  65. }
  66. }
  67. if (p>0 && z>0){
  68. change(matr,n,m);
  69. cout << "Измененная матрица:" << endl;
  70. output(matr,n,m);
  71. } else
  72. cout << "Матрица не изменилась." << endl;
  73. return 0;
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement