Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iomanip>
- #include <algorithm>
- #include <cmath>
- #include <string>
- #include <vector>
- #include <set>
- #include <ctime>
- using namespace std;
- vector <double> function(double start, double end, double step) {
- vector <double> sp;
- double y;
- cout << " X Y" << endl;
- for (double x = start; x <= end; x += step) {
- y = 3 * cos(5 * x) + 2 * sin(x);
- sp.push_back(y);
- cout << setw(12) << x << " " << setw(12) << y << endl;
- }
- return sp;
- }
- int max_el_position(vector <double> a, int n) {
- int ans = 0;
- int max_el = -100;
- for (int i = 0; i < n; i++) {
- if (a[i] >= max_el) {
- ans = i;
- max_el = a[i];
- }
- }
- return ans;
- }
- int min_el_position(vector <double> a, int n) {
- int ans = 0;
- int min_el = 100;
- for (int i = 0; i < n; i++) {
- if (a[i] <= min_el) {
- ans = i;
- min_el = a[i];
- }
- }
- return ans;
- }
- vector <double> make_C(vector <double> a, int n) {
- vector <double> c;
- for (int i = 0; i <= n; i++) {
- c.push_back(a[i]);
- }
- return c;
- }
- vector <double> make_D(vector <double> a, int n) {
- vector <double> d;
- for (int i = 0; i <= n; i++) {
- if (a[i] < 0) {
- d.push_back(a[i]);
- }
- }
- return d;
- }
- void print_list(vector <double> a, int n) {
- for (int i = 0; i < n; i++) {
- cout << setw(9) << a[i] << " ";
- }
- cout << endl;
- }
- vector <double> swap_el(vector <double> a, int i, int j) {
- double t1 = a[i], t2 = a[j];
- a[i] = t2;
- a[j] = t1;
- return a;
- }
- vector <double> shuffle(vector <double> a, int n) {
- for (int i = 0; i < n / 2; i++) {
- a = swap_el(a, i, n - i - 1);
- }
- return a;
- }
- vector <double> sort_vector(vector <double> a, int n) {
- for (int i = 0; i < n - 1; i++) {
- for (int j = i + 1; j < n; j++) {
- if (a[j] > a[i]) {
- a = swap_el(a, i, j);
- }
- }
- }
- return a;
- }
- vector <vector <double>> make_K(vector <double> c, vector <double> d, int n) {
- vector <vector <double>> k;
- double el;
- for (int i = 0; i < n; i++) {
- vector <double> a;
- for (int j = 0; j < n; j++) {
- if (i == j) {
- a.push_back(1);
- }
- else {
- el = c[i] * d[j] - c[j] * d[i];
- a.push_back(el);
- }
- }
- k.push_back(a);
- }
- return k;
- }
- int count_negative(vector <vector <double>> a, int n) {
- int cnt = 0;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < n; j++) {
- if (a[i][j] < 0) {
- cnt++;
- }
- }
- }
- return cnt;
- }
- void print_matrix(vector <vector <double>> a, int n) {
- for (int i = 0; i < n; i++) {
- print_list(a[i], n);
- }
- }
- vector <vector <double>> transpose_matrix(vector <vector <double>> k, int n) {
- for (int i = 0; i < n; i++) {
- for (int j = i; j < n; j++) {
- double t1 = k[i][j], t2 = k[j][i];
- k[i][j] = t2;
- k[j][i] = t1;
- }
- }
- return k;
- }
- int main() {
- const double PI = 3.141;
- setlocale(LC_ALL, "RUS");
- vector <double> A = function(-PI, PI, PI / 10);
- int min_el_pos = min_el_position(A, A.size());
- int max_el_pos = max_el_position(A, A.size());
- vector <double> C = make_C(A, min_el_pos);
- vector <double> D = make_D(A, max_el_pos);
- cout << "Массив C:" << endl;
- print_list(C, C.size());
- cout << "Массив C после изменений:" << endl;
- C = shuffle(C, C.size());
- print_list(C, C.size());
- cout << "Массив D:" << endl;
- print_list(D, D.size());
- cout << "Массив D после изменений:" << endl;
- D = sort_vector(D, D.size());
- print_list(D, D.size());
- int n;
- if (D.size() >= C.size()) {
- n = C.size();
- }
- else {
- n = D.size();
- }
- vector <vector <double>> K = make_K(C, D, n);
- cout << "Матрица K:" << endl;
- print_matrix(K, K.size());
- int cnt_neg = count_negative(K, K.size());
- if (cnt_neg * 3 > n * n) {
- cout << "Матрица K после изменений:" << endl;
- K = transpose_matrix(K, K.size());
- print_matrix(K, K.size());
- }
- cout << "Количество отрицательных элементов матрицы K: " << cnt_neg << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement