/
Michael2077
/
lab12_var12
Обзор
Документация
Войти
/
Michael2077
/
lab12_var12
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
lab12.cpp
172 строки
5 KB
Michael2077
laba№12,var12, отчет, задание 1-2
28 апр 2026, 00:29
Верифицирован
28 апр 2026, 00:29
7d560dd
Код
Авторство
О чём код?
// ������������ ������ �12, ������� 1, ������� 12 // ���� ������� ����� �����. ������� map, ��� ���� - ����� ��������� // ��������� ������, �������� - ������. ������������� �� ������ �� ��������. #include <iostream> #include <vector> #include <map> #include <algorithm> #include <random> #include <numeric> #include <iomanip> #include <functional> using namespace std; long long sumOfSquares(const vector<int>& row) { long long sum = 0; for (size_t j = 0; j < row.size(); ++j) { sum += static_cast<long long>(row[j]) * row[j]; } return sum; } void printRow(const vector<int>& vec) { cout << "["; for (size_t i = 0; i < vec.size(); ++i) { cout << setw(5) << vec[i]; if (i != vec.size() - 1) cout << ","; } cout << " ]"; } void printMatrix(const vector<vector<int>>& matrix) { cout << "\n=== �������� ������� " << matrix.size() << "x" << (matrix.empty() ? 0 : matrix[0].size()) << " ===" << endl; for (size_t i = 0; i < matrix.size(); ++i) { cout << " ������ " << i << ": "; printRow(matrix[i]); cout << " (����� ��.: " << sumOfSquares(matrix[i]) << ")" << endl; } } map<long long, vector<int>, greater<long long>> createSumSquaresMap( const vector<vector<int>>& matrix) { map<long long, vector<int>, greater<long long>> resultMap; for (size_t i = 0; i < matrix.size(); ++i) { long long key = sumOfSquares(matrix[i]); resultMap[key] = matrix[i]; } return resultMap; } void printMap(const map<long long, vector<int>, greater<long long>>& sortedMap) { cout << "\n=== MAP (����: ����� ��������� -> ��������: ������) ===" << endl; cout << " ������������� �� ������ �� ��������:" << endl; int idx = 1; for (map<long long, vector<int>, greater<long long>>::const_iterator it = sortedMap.begin(); it != sortedMap.end(); ++it) { cout << " " << idx++ << ") ���� = " << setw(10) << it->first << " | ������: "; printRow(it->second); cout << endl; } } void verifyResult(const map<long long, vector<int>, greater<long long>>& sortedMap) { cout << "\n=== �������������� ������������ ===" << endl; cout << " �������� ������� ������ (������ �������):" << endl; long long prev = LLONG_MAX; bool correct = true; int idx = 1; for (map<long long, vector<int>, greater<long long>>::const_iterator it = sortedMap.begin(); it != sortedMap.end(); ++it) { bool ok = (it->first <= prev); cout << " " << idx++ << ") " << it->first; if (it != sortedMap.begin()) { cout << (ok ? " <= �����������: ��" : " > �����������: ������!"); if (!ok) correct = false; } else { cout << " (������ �������)"; } cout << endl; prev = it->first; } cout << "\n �������� ������������ ������ ���������:" << endl; for (map<long long, vector<int>, greater<long long>>::const_iterator it = sortedMap.begin(); it != sortedMap.end(); ++it) { long long computed = sumOfSquares(it->second); bool match = (computed == it->first); cout << " ����=" << it->first << ", ����������� ����� ��.=" << computed << " -> " << (match ? "���������" : "�� ���������!") << endl; if (!match) correct = false; } cout << "\n ����: " << (correct ? "��������� ������" : "���������� ������!") << endl; } int main() { setlocale(LC_ALL, ""); random_device rd; mt19937 gen(rd()); uniform_int_distribution<int> dist(-10, 10); int M, N; cout << "������� ���������� ����� M: "; cin >> M; cout << "������� ���������� �������� N: "; cin >> N; if (M <= 0 || N <= 0) { cout << "������: ������� ������� ������ ���� ��������������!" << endl; return 1; } vector<vector<int>> matrix(M, vector<int>(N)); cout << "\n���������� ������� ���������� ������� � ��������� [-10, 10]..." << endl; for (int i = 0; i < M; ++i) for (int j = 0; j < N; ++j) matrix[i][j] = dist(gen); printMatrix(matrix); map<long long, vector<int>, greater<long long>> resultMap = createSumSquaresMap(matrix); printMap(resultMap); verifyResult(resultMap); cout << "\n=== ������� ���������� ===" << endl; cout << " ������ �������: " << M << "x" << N << endl; cout << " ����� � �������� �������: " << M << endl; cout << " ��������� � map: " << resultMap.size() << endl; if ((int)resultMap.size() < M) cout << " ����������: " << (M - (int)resultMap.size()) << " ����� ����� ���������� ����� ��������� � ���� ������������ � map." << endl; return 0; }