#include <iostream>
#include <vector>
#include <algorithm>
#include <iterator>
bool test (int n)
{
for (int i=2; i<n; i++)
if (!(n%i)) return false;
return true;
}
void show (std::vector <int> &v)
{
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
int main ()
{
const int size=20;
std::vector <int> Z;
std::vector <int> Y(size);
srand(time(NULL));
for (int i=0; i<size; i++)
Z.push_back (rand()%20+1);
std::cout << "Start matrix Z:\n";
show (Z);
reverse_copy (Z.begin(), Z.end(), Y.begin());
std::cout << "Start matrix Y:\n";
show (Y);
std::vector<int>::iterator it;
while ((it=std::find_if (Z.begin(), Z.end(), test)) != Z.end())
Z.erase (it);
std::cout << "Matrix Z after remove:\n";
show (Z);
int Z_min=*std::min_element (Z.begin(), Z.end());
int Y_min=*std::min_element (Y.begin(), Y.end());
int Z_max=*std::max_element (Z.begin(), Z.end());
int Y_max=*std::max_element (Y.begin(), Y.end());
if (Z_max > Y_max) std::cout << "Max_Z > Max_Y" << std::endl;
else std::cout << "Max_Z < Max_Y" << std::endl;
if (Z_min > Y_min) std::cout << "Min_Z > Min_Y" << std::endl;
else std::cout << "Min_Z < Min_Y" << std::endl;
system ("pause");
return 0;
}
Следующий вариант
#include <iostream>
#include <math.h>
using namespace std;
const int sz=20;
bool test (int n);
int main()
{
setlocale (LC_ALL, "Russian");
int n, a[sz], i, buf, j=0, k, t, y[sz]={0};
double b[sz]={0};
bool flag;
cout<<"Введите количество элементов: ";
cin>>n;
cout<<"Введите "<<n<<" элементов: \n";
for (i=0; i<n; i++)
cin>>a[i];
for (i=0; i<n; i++)
{
j=0;
k=0;
buf=a[i];
while (buf!=0)
{
b[j++]=buf%10;
buf/=10;
k++;
}
for (j=0, t=k-1; j<k; j++, t--)
y[i]+= b[j] * pow (10.0, t);
}
cout<<"Полученный массив:\n";
for (i=0; i<n; i++)
cout<<y[i]<<" ";
for (i=0; i<n; i++)
{
flag=test (a[i]);
if (flag==true)
{
for (j=i; j<n; j++)
a[j]=a[j+1];
n--;
i--;
}
}
cout<<endl<<endl;
cout<<"Без простых чисел:\n";
for (i=0; i<n; i++)
cout<<y[i]<<" ";
cout<<endl<<endl;
}
bool test (int n)
{
for (int i=2; i<n/2+1; i++)
if ((n%i)==0)
return false;
return true;
}
Следующий вариант
#include <iostream>
#include <cmath>
#include <vector>
#include <algorithm>
#include <iterator>
bool test (int n)
{
for (int i=2; i<=sqrt(n); i++)
if (!(n%i)) return false;
return true;
}
void show (std::vector <int> &v)
{
std::copy(v.begin(), v.end(), std::ostream_iterator<int>(std::cout, " "));
std::cout << std::endl;
}
int main ()
{
const int size=20;
std::vector <int> Z;
std::vector <int> Y(size);
srand(time(NULL));
for (int i=0; i<size; i++)
Z.push_back (rand()%20+1);
std::cout << "Start matrix Z:\n";
show (Z);
reverse_copy (Z.begin(), Z.end(), Y.begin());
std::cout << "Start matrix Y:\n";
show (Y);
std::vector<int>::iterator it;
while ((it=std::find_if (Z.begin(), Z.end(), test)) != Z.end())
Z.erase (it);
std::cout << "Matrix Z after remove:\n";
show (Z);
int Z_min=*std::min_element (Z.begin(), Z.end());
int Y_min=*std::min_element (Y.begin(), Y.end());
int Z_max=*std::max_element (Z.begin(), Z.end());
int Y_max=*std::max_element (Y.begin(), Y.end());
if (Z_max > Y_max) std::cout << "Max_Z > Max_Y" << std::endl;
else std::cout << "Max_Z < Max_Y" << std::endl;
if (Z_min > Y_min) std::cout << "Min_Z > Min_Y" << std::endl;
else std::cout << "Min_Z < Min_Y" << std::endl;
system ("pause");
return 0;
}