#include <iostream>
using namespace std;
int main(){
const int H = 5;
const int W = 5;
int m[H] = {0};
int matrix[H][W] = {
{0,0,0,0,0},
{0,0,0,0,1},
{0,0,1,1,1},
{0,0,0,1,1},
{0,1,1,1,1}
};
int i,j;
for( i = 0 ; i < H ; i++ )
for( j = 0 ; j < W ; j++ )if(matrix[j]==0)m++;
for( i = 0 ; i < H -1 ; i++ )
for( j = i + 1 ; j < W ; j++)
if(m[j]<m){
int tmp = m;
m=m[j];
m[j] = tmp;
int p;
for( p = 0 ; p < W ; p++ ){
tmp = matrix[p];
matrix[p]=matrix[j][p];
matrix[j][p]=tmp;
}
}
for( i = 0 ; i < H ; i++ ){
for( j = 0 ; j < W ; j++)cout<<matrix[j]<<' ';
cout<<endl;
}
return 0;
}
C++:
#include <iostream>
using namespace std;
int main(){
const int H = 5;
const int W = 5;
int m[H] = {0};
int matrix[H][W] = {
{0,0,0,0,0},
{0,0,0,0,1},
{0,0,1,1,1},
{0,0,0,1,1},
{0,1,1,1,1}
};
int i,j;
for( i = 0 ; i < H ; i++ )
for( j = 0 ; j < W ; j++ )if(matrix[i][j]==0)m[i]++;
for( i = 0 ; i < H -1 ; i++ )
for( j = i + 1 ; j < W ; j++)
if(m[j]<m[i]){
int tmp = m[i];
m[i]=m[j];
m[j] = tmp;
int p;
for( p = 0 ; p < W ; p++ ){
tmp = matrix[i][p];
matrix[i][p]=matrix[j][p];
matrix[j][p]=tmp;
}
}
for( i = 0 ; i < H ; i++ ){
for( j = 0 ; j < W ; j++)cout<<matrix[i][j]<<' ';
cout<<endl;
}
return 0;
}