9 / 9 / 7
Регистрация: 04.05.2015
Сообщений: 34
|
11.05.2015, 15:56
|
3
|
Сообщение было отмечено Rodionusa как решение
Решение
Rodionusa, вот собственный говнокодец для подсчёта количества локальных минимумов :-)
Тесчено на MSVS2013upd4; clang-3.4-1
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
| // MatrixLocalMinimum.cpp
#include "matrix.hpp"
#include <iostream>
void printMatrix(const QMatrix& right_matrix);
int
main(int argc, char* argv[])
{
QMatrix right_matrix{
{ 3, 4, -5},
{-2, 1, 7},
{ 2, 0, 8}
};
printMatrix(right_matrix);
std::printf("local minimums: %d\n",
(int) right_matrix.countLocalMininums());
return 0;
}
void
printMatrix(const QMatrix& right_matrix)
{
for (std::size_t ii{ 0 }; ii != right_matrix.getDimensionSize(); ++ii)
{
for (std::size_t jj{ 0 }; jj != right_matrix.getDimensionSize(); ++jj)
{
std::printf("%6d", right_matrix[ ii ][ jj ]);
}
std::puts("");
}
} |
|
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
| // matrix.hpp
#ifndef MATRIX_HEADER_GUARD
# define MATRIX_HEADER_GUARD
# include <cstdlib>
# include <initializer_list>
# include <new>
using MatrixInitializerList = std::initializer_list<std::initializer_list<int>>;
class QMatrix {
public: // ctors & dtor:
QMatrix(std::size_t dim_size);
QMatrix(MatrixInitializerList init_list);
~QMatrix();
public: // methods:
int multiplyElementsOfRowsWithoutNegatives() const;
std::size_t countColumnsWithoutNulls() const;
int* operator [] (std::size_t ii) const;
QMatrix& setElement(std::size_t i, std::size_t j, int el);
const std::size_t& getDimensionSize() const;
std::size_t countLocalMininums() const;
private: // fields:
std::size_t dimension;
int** rows;
};
#endif |
|
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
| // matrix.cpp
#include "matrix.hpp"
QMatrix::QMatrix(std::size_t dim_size)
: dimension{ dim_size }
, rows{ new int*[ dim_size ]{ nullptr } }
{
if (dim_size < 2)
{
delete[] this->rows;
throw 1;
}
try
{
for (std::size_t ii{ 0 }; ii != dim_size; ++ii)
{
this->rows[ ii ] = new int[ dim_size ]{};
}
}
catch (std::bad_alloc& bad_alloc)
{
for (std::size_t ii{ 0 }; ii != dim_size; ++ii)
{
delete[] this->rows[ ii ];
}
throw;
}
}
QMatrix::~QMatrix()
{
for (std::size_t ii{ 0 }; ii != this->dimension; ++ii)
{
delete[] this->rows[ ii ];
}
delete[] this->rows;
}
QMatrix::QMatrix(MatrixInitializerList init_list)
: QMatrix{ init_list.size() }
{
std::size_t ii{ 0 };
for (const auto& row : init_list)
{
std::size_t jj{ 0 };
for (const auto& el : row)
{
this->setElement(ii, jj, el);
++jj;
}
++ii;
}
}
int
QMatrix::multiplyElementsOfRowsWithoutNegatives() const
{
int result{ 1 };
for (std::size_t ii{ 0 }; ii != this->dimension; ++ii)
{
int row_product{ 1 };
for (std::size_t jj{ 0 }; jj != this->dimension; ++jj)
{
if (this->rows[ ii ][ jj ] < 0)
{
row_product = -1;
break;
}
row_product *= this->rows[ ii ][ jj ];
}
if (row_product != -1)
{
result *= row_product;
}
}
return result;
}
std::size_t
QMatrix::countColumnsWithoutNulls() const
{
std::size_t colums_count{ 0 };
for (decltype(this->dimension) jj{ 0 }; jj != this->dimension; ++jj)
{
bool no_null{ true };
for (decltype(this->dimension) ii{ 0 }; ii != this->dimension; ++ii)
{
if (!this->rows[ ii ][ jj ])
{
no_null = false;
break;
}
}
if (no_null)
{
++colums_count;
}
}
return colums_count;
}
int*
QMatrix::operator [] (std::size_t ii) const
{
if (ii > this->dimension)
{
throw 3;
}
return this->rows[ ii ];
}
QMatrix&
QMatrix::setElement(std::size_t i, std::size_t j, int el)
{
if (i > this->dimension || j > this->dimension)
{
throw 2;
}
this->rows[ i ][ j ] = el;
return *this;
}
const std::size_t&
QMatrix::getDimensionSize() const
{
return this->dimension;
}
inline
void
incrementIf(char& around, bool b)
{
if (b)
{
++around;
}
}
std::size_t
QMatrix::countLocalMininums() const
{
std::size_t result{ 0 };
for (std::size_t ii{ 0 }; ii != this->dimension; ++ii)
{
for (std::size_t jj{ 0 }; jj != this->dimension; ++jj)
{
char around{ 0 };
incrementIf(around, ii != 0 &&
this->rows[ ii ][ jj ] < this->rows[ ii - 1 ][ jj ]);
incrementIf(around, jj != 0 &&
this->rows[ ii ][ jj ] < this->rows[ ii ][ jj - 1 ]);
incrementIf(around, jj != 0 && ii != 0 &&
this->rows[ ii ][ jj ] < this->rows[ ii - 1 ][ jj - 1 ]);
incrementIf(around, jj != 0 && ii < this->dimension - 1 &&
this->rows[ ii ][ jj ] < this->rows[ ii + 1 ][ jj - 1 ]);
incrementIf(around, jj < this->dimension - 1 &&
ii < this->dimension - 1 &&
this->rows[ ii ][ jj ] < this->rows[ ii + 1 ][ jj + 1 ]);
incrementIf(around, ii != 0 && jj < this->dimension - 1 &&
this->rows[ ii ][ jj ] < this->rows[ ii - 1 ][ jj + 1 ]);
incrementIf(around, jj < this->dimension - 1 &&
this->rows[ ii ][ jj ] < this->rows[ ii ][ jj + 1 ]);
incrementIf(around, ii < this->dimension - 1 &&
this->rows[ ii ][ jj ] < this->rows[ ii + 1 ][ jj ]);
if (((ii == 0 || jj == 0) ||
(ii == (this->dimension - 1) || jj == (this->dimension - 1)))
&&
around == 5)
{
++result;
}
else if (around == 7)
{
++result;
}
}
}
return result;
} |
|
0
|