По мотивам этой темы. Автор кода Invader0x7F.
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
| #include <ctime>
#include <vector>
#include <iostream>
#include <random>
typedef std::vector<int> Row;
typedef std::vector<Row> Matrix;
const size_t N = 10;
const int FROM_NUM = 1;
const int TO_NUM = 9;
std::mt19937 mt( ( unsigned )time( nullptr ) );
std::uniform_int_distribution<int> dist( FROM_NUM, TO_NUM );
void Init( Matrix& matrix )
{
Row row( N );
for ( size_t i = 0; i < N; ++i )
{
for (size_t j = 0; j < N; ++j )
row[j] = dist( mt );
matrix.push_back( row );
}
}
void Display( const Matrix& matrix )
{
for ( const Row& row : matrix )
{
for ( const int n_item : row )
{
std::cout << n_item << " ";
}
std::cout << std::endl;
}
std::cout << std::endl;
}
void Compute( Matrix& matrix )
{
const size_t reducedFirstRowSize = matrix[0].size() - 1;
const size_t firstRowSize = matrix[0].size();
for ( size_t d = 0; d < reducedFirstRowSize; ++d )
{
size_t sum = 0;
for ( size_t i = 0, j = d + 1; j < firstRowSize; ++i, ++j )
{
int n = *( ( matrix.begin() + i )->begin() + j );
sum += n;
std::cout << n << " ";
}
std::cout << "sum = " << sum << std::endl;
}
}
int main()
{
Matrix matrix;
Init( matrix );
Display( matrix );
Compute( matrix );
//std::cin.get();
//std::cin.get();
return 0;
} |
|
|