Другие языки программирования и технологии

С++ Нужно удалить все столбцы, содержащие только четные элементы.

Я сделал, но у меня строки удаляются с чётными элементами, а не столбцы...
#include

using namespace std;

int main()
{
setlocale(LC_ALL,"RUSSIAN");
int n,m;
cout<<"n=";cin>>n;
cout<<"m=";cin>>m;
int **a=new int*[n];
for(int i=0;i<n;i++)
a[i]=new int[m];
for(int i=0;i<n;i++,cout<<endl)
for(int j=0;j<m;j++){
cout<<"a["<<i<<"]["<<j<<"]= ";
cin>>a[i][j];
}
cout<<"Массив:" <<endl;
for(int i=0;i<n;i++,cout<<endl)
for(int j=0;j<m;j++)
cout<<a[i][j]<<" ";

for(int i=0;i<n;i++)
{
bool A=true;
for(int j=0;j<m;j++)
if(a[i][j]%2!=0){
A=false;break;
}

if(A){
for(int k=i;k<n-1;k++)
for(int j=0;j<m;j++)
a[k][j]=a[k+1][j];
n--;i--;

}
}
cout<<"Результат:" <<endl;
for(int i=0;i<n;i++,cout<<endl)
for(int j=0;j<m;j++){
cout<<a[i][j]<<" ";
}

for(int i=0;i<n;i++)
delete[]a[i];
delete[]a;
return 0;
}
Raman G?k
Raman G?k
4 142
#include <iostream>
#include <windows.h>
#include <vector>
#include <algorithm>
#include <string>
#include <iomanip>

using namespace std;

int main()
{
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
system("color 0A");

size_t n, m;
cout << "n = ";
cin >> n;
cout << "m = ";
cin >> m;
vector<vector<int>> v(n);
for (auto &value : v)
{
vector<int> str(m);
value = str;
}
cout << "Заполняем матрицу" << endl;
for (size_t u = 0u; u < n; ++u)
{
for (size_t p = 0u; p < m; ++p)
{
cout << "a[" << u << "][" << p << "]= ";
cin >> v[u][p];
}
}
vector<size_t> del;
for (size_t u = 0u; u < m; ++u)
{
bool flag = true;
for (size_t p = 0u; p < n; ++p)
{
if (v[p][u] & 1u)
{
flag = false;
break;
}
}
if (flag)
{
del.push_back(u);
}
}
for (size_t u = 0u; u < n; ++u)
{
for (auto it = del.rbegin(); it != del.rend(); ++it)
{
v[u].erase(v[u].begin() + *it);
}
}
cout << "Содержание обработанной матрицы" << endl;
for (const auto &str : v)
{
for (const auto &value : str)
{
cout << setw(5) << value;
}
cout << endl;
}
cout << endl;

system("pause");
return 0;
}
Виталик Орлов
Виталик Орлов
8 552
Лучший ответ

Похожие вопросы