原文:

给定一个有 n 行 m 列的矩阵。任务是根据每列中不断增加的零数打印给定矩阵的列索引。 例如,如果第一列包含 2 个零,第二列包含 1 个零,第三列不包含任何零。那么输出将是 3,2,1。

:矩阵被认为具有基于 1 的索引。

示例:

input : mat[n][m] ={{0, 0, 0},
                    {0, 2, 0},
                    {0, 1, 1},
                    {1, 1, 1}};
output : 2 3 1 
no. of zeroes in first col: 3
no. of zeroes in second col: 1
no of zeroes in third col: 2
therefore, sorted order of count is 1 2 3
and their corresponding column numbers are, 2 3 1
input: mat[n][m] ={{0, 0, 0},
                    {0, 0, 3},
                    {0, 1, 1}};
output : 3 2 1 

接近:

  1. 创建一个向量对来存储每列中的零计数。其中对的第一个元素是计数,对的第二个元素是对应的列索引。
  2. 按列遍历矩阵。
  3. 在成对向量中插入每列的零计数。
  4. 根据零的计数对向量进行排序。
  5. 打印该对中的第二个元素,该元素包含根据零的计数排序的列的索引。

下面是上述方法的实现:

c

// c   program to print the index of columns
// of the given matrix based on the
// increasing number of zeroes in each column
#include 
using namespace std;
#define n 4 // rows
#define m 3 // columns
// function to print the index of columns
// of the given matrix based on the
// increasing number of zeroes in each column
void printcolumnsorted(int mat[n][m])
{
    // vector of pair to store count of zeroes
    // in each column.
    // first element of pair is count
    // second element of pair is column index
    vector > colzerocount;
    // traverse the matrix column wise
    for (int i = 0; i < m; i  ) {
        int count = 0;
        for (int j = 0; j < n; j  ) {
            if (mat[j][i] == 0)
                count  ;
        }
        // insert the count of zeroes for each column
        // in the vector of pair
        colzerocount.push_back(make_pair(count, i));
    }
    // sort the vector of pair according to the
    // count of zeroes
    sort(colzerocount.begin(), colzerocount.end());
    // print the second element of the pair which
    // contain indexes of the sorted vector of pair
    for (int i = 0; i < m; i  )
        cout << colzerocount[i].second   1 << " ";
}
// driver code
int main()
{
    int mat[n][m] = { { 0, 0, 0 },
                      { 0, 2, 0 },
                      { 0, 1, 1 },
                      { 1, 1, 1 } };
    printcolumnsorted(mat);
    return 0;
}

java 语言(一种计算机语言,尤用于创建网站)

// java program to print the index of columns
// of the given matrix based on the increasing
// number of zeroes in each column
import java.io.*;
import java.util.*;
class gfg{
static int n = 4;
static int m = 3;
// function to print the index of columns
// of the given matrix based on the increasing
// number of zeroes in each column
static void printcolumnsorted(int[][] mat)
{
    // vector of pair to store count of zeroes
    // in each column.first element of pair is
    // count.second element of pair is column index
    arraylist<
    arraylist> colzerocount = new arraylist<
                                           arraylist>();
    // traverse the matrix column wise
    for(int i = 0; i < m; i  )
    {
        int count = 0;
        for(int j = 0; j < n; j  )
        {
            if (mat[j][i] == 0)
            {
                count  ;
            }
        }
        // insert the count of zeroes for
        // each column in the vector of pair
        colzerocount.add(new arraylist(
            arrays.aslist(count,i)));
    }
    // sort the vector of pair according to the
    // count of zeroes
    collections.sort(colzerocount,
                     new comparator>()
    {   
        @override
        public int compare(arraylist o1,
                           arraylist o2)
        {
            return o1.get(0).compareto(o2.get(0));
        }              
    });
    // print the second element of the pair which
    // contain indexes of the sorted vector of pair
    for(int i = 0; i < m; i  )
    {
        system.out.print(
            (colzerocount.get(i).get(1)   1)   " ");
    }
}
// driver code
public static void main(string[] args)
{
    int[][] mat = { { 0, 0, 0 }, { 0, 2, 0 },
                    { 0, 1, 1 }, { 1, 1, 1 } };
    printcolumnsorted(mat);
}
}
// this code is contributed by avanitrachhadiya2155

python 3

# python3 program to print the index of
# columns of the given matrix based
# on the increasing number of zeroes
# in each column
# rows
n = 4
# columns
m = 3
# function to print the index of columns
# of the given matrix based on the
# increasing number of zeroes in
# each column
def printcolumnsorted(mat):
    # vector of pair to store count
    # of zeroes in each column.
    # first element of pair is count
    # second element of pair is column index
    colzerocount = []
    # traverse the matrix column wise
    for i in range(m):
        count = 0
        for j in range(n):
            if (mat[j][i] == 0):
                count  = 1
        # insert the count of zeroes for
        # each column in the vector of pair
        colzerocount.append((count, i))
    # sort the vector of pair according
    # to the count of zeroes
    colzerocount = sorted(colzerocount)
    # print the second element of the
    # pair which contain indexes of the
    # sorted vector of pair
    for i in range(m):
        print(colzerocount[i][1]   1, end = " ")
# driver code
if __name__ == '__main__':
    mat = [ [ 0, 0, 0 ],
            [ 0, 2, 0 ],
            [ 0, 1, 1 ],
            [ 1, 1, 1 ] ]
    printcolumnsorted(mat)
# this code is contributed mohit kumar 29

java 描述语言


output: 

2 3 1