#include <iostream>
#include <vector>
#include <algorithm>
#include <functional>
const double M_PI = 3.14159265358979323846;
using namespace std;
vector <double> A,B;
vector <double> Z;
double **E;
double Funct1 (double x)
{
double Result = 3.0 * cos(5*x) + sin(x);
return Result;
}
void Print (double Val)
{
cout<<Val<<endl;
}
void InToMassB(vector<double>All,double SrFun)
{
for (int i = 0; i<All.size(); i++)
{
if (All[i] < SrFun )
{
B.push_back(All[i]);
}
}
}
int main(void)
{
double Iterator = -M_PI;
double MaxIterator = M_PI;
double StepIter = M_PI / 10;
double SredFunc = 0;
int IterCount = 0;
while (Iterator <= MaxIterator)
{
double FValue = Funct1(Iterator);
SredFunc += FValue;
IterCount++;
if (FValue != 0)
{
A.push_back(FValue);
}
Z.push_back(FValue);
Iterator += StepIter;
}
SredFunc = (SredFunc/Iterator) /2;
InToMassB(Z,SredFunc);
sort(A.begin(),A.end());
sort(B.begin(),B.end(),greater<double>());
int Min_Razm;
(A.size() > B.size()) ? Min_Razm = B.size(): Min_Razm = A.size();
int RowMaxPlus = 0;
int PlusEl = -1;
E = new double* [Min_Razm];
for (int i=0; i<Min_Razm;i++)
{
int PlusElements = 0;
E[i] = new double [Min_Razm];
for (int j=0; j<Min_Razm;j++)
{
if (A[i] > B [j]) E[i][j] = A[i] - B[j];
if (A[i] < B [j]) E[i][j] = A[i] + B[j];
if (A[i] == B [j]) E[i][j]= 0;
if (E[i][j] > 0) PlusElements++;
}
if (PlusElements > PlusEl)
{
RowMaxPlus = i;
PlusEl = PlusElements;
}
}
for (int i=0; i<Min_Razm;i++)
{
double buffer = 0;
buffer = E[RowMaxPlus][i];
E[RowMaxPlus][i] = E[i][RowMaxPlus];
E[i][RowMaxPlus] = buffer;
}
cout<<"A:"<<endl;
for_each(A.begin(),A.end(),Print);
cout<<endl<<"B:"<<endl;
for_each(B.begin(),B.end(),Print);
cout<<"E:"<<endl;
for (int i=0; i<Min_Razm;i++)
{
for (int j=0; j<Min_Razm;j++)
{
cout<<E[i][j]<<" ";
}
cout<<endl;
}
}