Advertisement
Gamerkin

Работа №12

Dec 24th, 2022
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.29 KB | None | 0 0
  1. #include <iostream>
  2. #include <bits/stdc++.h>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. int M, buff;
  10. char foo;
  11. vector<int> arr1;
  12. vector<int> arr2;
  13.  
  14. while (true) {
  15. cin >> M;
  16. if ((M < 2) || (M > 5)) continue;
  17. else break;
  18. }
  19.  
  20. int matr[M][M];
  21. cin >> foo;
  22.  
  23. if (foo == 'r') {
  24. for (int i = 0; i < M; i++)
  25. for (int j = 0; j < M; j++) matr[i][j] = 1 + (rand() % 100);
  26. }
  27. else {
  28. for (int i = 0; i < M; i++)
  29. for (int j = 0; j < M; j++) cin >> matr[i][j];
  30. }
  31.  
  32. for (int i = 0; i < M; i++) {
  33. for (int j = 0; j < M; j++) cout << matr[i][j] << " ";
  34. cout << endl;
  35. }
  36. cout << endl;
  37.  
  38. for (int i = 0; i < M; i++) {
  39. buff = 0;
  40. for (int j = 0; j < M; j++) {
  41. buff += matr[i][j];
  42. }
  43. arr1.push_back(buff);
  44. }
  45.  
  46. for (int i = 0; i < M; i++) {
  47. buff = 0;
  48. for (int j = 0; j < M; j++) {
  49. buff += matr[j][i];
  50. }
  51. arr2.push_back(buff);
  52. }
  53.  
  54. sort(arr1.begin(), arr1.end(), greater<int>());
  55. sort(arr2.begin(), arr2.end());
  56. arr1.insert(arr1.end(), arr2.begin(), arr2.end());
  57. for (int i = 0; i < arr1.size(); i++) cout << arr1[i] << " ";
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement