Advertisement
NickAndNick

Поиск строки матрицы с максимальной суммой

Jun 16th, 2013
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.00 KB | None | 0 0
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <ctime>
  4.  
  5. using namespace std;
  6.  
  7. typedef unsigned int index_t;
  8.  
  9. int ** random(const size_t, const size_t);
  10. void show(int **, const size_t, const size_t);
  11. void freemem(int **, const size_t);
  12. long long summx(int *, const size_t);
  13. bool exists(int *, const size_t);
  14.  
  15. int main() {
  16.     setlocale(LC_CTYPE, ".1251");
  17.     cout << " Введите M: "; size_t m; cin >> m;
  18.     cout << " Введите N: "; size_t n; cin >> n;
  19.     cout << endl;
  20.     int ** matrix = NULL;
  21.     if (matrix = random(m, n)) {
  22.         show(matrix, m, n);
  23.         long long max = LLONG_MIN, current;
  24.         index_t index = m;
  25.         for (index_t r = 0; r < m; r++) {
  26.             if (exists(matrix[r], n)) {
  27.                 current = summx(matrix[r], n);
  28.                 if (current > max) {
  29.                     max = current;
  30.                     index = r;
  31.                 }
  32.             }
  33.         }
  34.         if (index < m) {
  35.             cout << " Наибольшая сумма в " << index + 1 << "-й строке." << endl
  36.                  << " Сумма равна: " << max << endl;
  37.         } else cout << "\a Эксперитмент не удался!" << endl;
  38.         freemem(matrix, m);
  39.         matrix = NULL;
  40.     }
  41.     cin.get(); cin.get();
  42.     return 0;
  43. }
  44.  
  45. bool exists(int * _v, const size_t _n) {
  46.     bool ex = false;
  47.     if (_v && _n) ex = true;
  48.     return ex;
  49. }
  50.  
  51. long long summx(int * _v, const size_t _n) {
  52.     long long sum = 0;
  53.     for (index_t n = 0; n < _n; n++)
  54.         sum += _v[n];
  55.     return sum;
  56. }
  57.  
  58. void freemem(int ** _mx, const size_t _m) {
  59.     if (_mx && _m) {
  60.         for (index_t m = 0; m < _m; m++)
  61.             if (_mx[m])
  62.                 delete[] _mx[m];
  63.         delete[] _mx;
  64.     }
  65. }
  66.  
  67. void show(int ** _mx, const size_t _m, const size_t _n) {
  68.     if (_mx && _m) {
  69.         for (index_t m = 0; m < _m; m++, cout << endl) {
  70.             if (exists(_mx[m], _n)) {
  71.                 streamsize w = 3;
  72.                 for (index_t n = 0; n < _n; n++)
  73.                     cout << setw(w) << _mx[m][n];
  74.             } else cout << "\a Строка не содержит данных!";
  75.         }
  76.     } else cout << "\a Матрица несуществует!";
  77.     cout << endl;
  78. }
  79.  
  80. int ** random(const size_t _m, const size_t _n) {
  81.     int ** matrix = NULL;
  82.     if (matrix = new (nothrow) int * [_m]) {
  83.         srand(unsigned(time(NULL)));
  84.         for (index_t m = 0; m < _m; m++) {
  85.             if (matrix[m] = new (nothrow) int [_n]) {
  86.                 for (index_t n = 0; n < _n; n++)
  87.                     matrix[m][n] = 1 + rand() % 99;
  88.             } else {
  89.                 for (index_t k = 0; k < m; k++)
  90.                     delete[] matrix[k];
  91.                 delete[] matrix;
  92.                 matrix = NULL;
  93.             }
  94.             if (!matrix) {
  95.                 cout << "\a Не удалось выделить память под матрицу!" << endl;
  96.                 break;
  97.             }
  98.         }
  99.     }
  100.     return matrix;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement