Removal of array elements- solution
domain: arrays, transforming arrays
#include <iostream>
using namespace std;
int main()
{
int n, z=0, m, j;
bool ima=false;
cin>>n;
int a[n], b[n];
/* Reading elemens of array*/
for(int i = 0; i < n; i++)
{
cin>>a[ i ];
}
m=n;
j=0;
do
{
ima=false;
for(int i=0; i<m; i++)
{
/* If the elements of a series of partitions are not copied into the array b*/
if(m%a[i]==0)
{
ima=true;
continue; // the skip cycle, the code below is not executed for the current cycle, but goes to the next
}
else
{
b[j++]=a[ i ];
// cout<<b[j-1]<<" ";
}
}
m=j;
/*rewrites the string elements into a array, preparing for the next cycle*/
for(int i=0; i<m; i++){
a[ i ]=b[ i ];
}
j=0;
}
while(ima);
// cout<<m<<endl;
//It collects the elements of the array b
for(int i=0; i<m; i++)
{
z=z+b[i];
// cout<<b[i]<<endl;
}
cout<<z<<endl;
return 0;
}
using namespace std;
int main()
{
int n, z=0, m, j;
bool ima=false;
cin>>n;
int a[n], b[n];
/* Reading elemens of array*/
for(int i = 0; i < n; i++)
{
cin>>a[ i ];
}
m=n;
j=0;
do
{
ima=false;
for(int i=0; i<m; i++)
{
/* If the elements of a series of partitions are not copied into the array b*/
if(m%a[i]==0)
{
ima=true;
continue; // the skip cycle, the code below is not executed for the current cycle, but goes to the next
}
else
{
b[j++]=a[ i ];
// cout<<b[j-1]<<" ";
}
}
m=j;
/*rewrites the string elements into a array, preparing for the next cycle*/
for(int i=0; i<m; i++){
a[ i ]=b[ i ];
}
j=0;
}
while(ima);
// cout<<m<<endl;
//It collects the elements of the array b
for(int i=0; i<m; i++)
{
z=z+b[i];
// cout<<b[i]<<endl;
}
cout<<z<<endl;
return 0;
}
© 2019 by Slobodan izprogramiranja.weebly.com
All rights reserved