Repository navigation
Expand file tree
/
Copy pathProga8-2.cpp
More file actions
61 lines (52 loc) · 1.68 KB
/
Copy pathProga8-2.cpp
File metadata and controls
61 lines (52 loc) · 1.68 KB
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
#include <iostream>
#include <vector>
#include <limits>
int main() {
setlocale(LC_ALL, "RUS");
int n;
std::cout << "Ââåäèòå ðàçìåð êâàäðàòíîé ìàòðèöû (n x n): ";
std::cin >> n;
std::vector<std::vector<int>> matrix(n, std::vector<int>(n));
std::cout << "Ââåäèòå ýëåìåíòû ìàòðèöû:" << std::endl;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
std::cin >> matrix[i][j];
}
}
std::vector<int> columnSums(n, 0);
std::vector<bool> hasNegative(n, false);
for (int j = 0; j < n; ++j) {
for (int i = 0; i < n; ++i) {
if (matrix[i][j] < 0) {
hasNegative[j] = true;
break;
}
}
if (!hasNegative[j]) {
for (int i = 0; i < n; ++i) {
columnSums[j] += matrix[i][j];
}
}
}
std::cout << "Ñóììà ýëåìåíòîâ â ñòîëáöàõ áåç îòðèöàòåëüíûõ çíà÷åíèé:" << std::endl;
for (int j = 0; j < n; ++j) {
if (!hasNegative[j]) {
std::cout << "Ñòîëáåö " << j + 1 << ": " << columnSums[j] << std::endl;
}
}
int minSum = std::numeric_limits<int>::max();
for (int k = -n + 1; k < n; ++k) {
int currentSum = 0;
for (int i = 0; i < n; ++i) {
int j = n - 1 - i + k;
if (j >= 0 && j < n) {
currentSum += abs(matrix[i][j]);
}
}
if (currentSum < minSum) {
minSum = currentSum;
}
}
std::cout << "Ìèíèìóì ñðåäè ñóìì ìîäóëåé ýëåìåíòîâ äèàãîíàëåé, ïàðàëëåëüíûõ ïîáî÷íîé äèàãîíàëè: " << minSum << std::endl;
return 0;
}