📌  相关文章
📜  放大矩阵,使每个元素出现在R行和C列中

📅  最后修改于: 2021-05-17 22:05:41             🧑  作者: Mango

给定大小为N x M的矩阵arr [] [] ,并且有两个数字RC ,任务是放大此矩阵,以使原始矩阵的每个元素出现在放大矩阵的R行和C列中。

例子:

方法:可以通过固定行数或列数来解决此问题。在本文中,行数是固定的。因此,对于维度为N x M的任何矩阵,最终答案中的行数将为R。
想法是首先将给定的矩阵转换为一维数组(即,将给定的矩阵展平)。现在,对于扁平化矩阵中的每个元素,将值附加到最终扩大后的矩阵的R行和C列中。

下面是上述方法的实现:

C++
// C++ program to enlarge a matrix
// such that each element occurs in
// R rows and C columns
#include 
using namespace std;
 
// Function to convert the matrix
// into a 1-D array
vector oneD(vector> &matrix){
      
    // Variable to store the
    // given matrix
    vector m;
  
    // Iterating through all the
    // elements of the matrix
    for(int i = 0; i < matrix.size(); ++i)
        for(int j = 0; j < matrix[0].size(); ++j)
            // Adding the element into the
            // defined matrix
            m.push_back(matrix[i][j]);
  
    return m;
}
  
// Function to enlarge a matrix
// such that each element occurs
// in R rows and C columns
vector> dimensions(int R, int C, vector matrix) {
      
    // Initializing the enlarged
    // matrix
    vector> ans(R+1);
 
    // Variables to iterate over
    // the matrix
    int ctr = 0;
    int r = 0;
      
    while(ctr < matrix.size()) {
  
        // To keep the rows in the
        // range [0, R]
        r = r % R;
        int c = 0;
        while(c < C) {            
            // In order to fix the number
            // of columns, the above r
            // statement can be commented 
            // and this can be uncommented
            // c = c % C
               for(int i = 0; i < R; ++i)
                   ans[i].push_back(matrix[ctr]);
                
               c += 1;
          }
        ctr += 1;
        r += 1;
    }
 
    return ans;
}
     
// Driver code
int main() {
 
    vector> arr = {{1, 2}, {3, 4}};
    int R = 2, C = 3;
  
    vector> ans = dimensions(R, C, oneD(arr));
      
    // Print the enlarged matrix
    for(int i = 0; i < ans.size(); ++i) {
     
        for(int j = 0; j < ans[i].size(); ++j) {
            cout << ans[i][j] << " ";
        }
     
        cout<


Java
// Java program to enlarge a matrix
// such that each element occurs in
// R rows and C columns
import java.io.*;
import java.util.*;
 
class GFG{
 
// Function to convert the matrix
// into a 1-D array
static ArrayListoneD(
    ArrayList> matrix)
{
     
    // Variable to store the
    // given matrix
    ArrayList m = new ArrayList();
 
    // Iterating through all the
    // elements of the matrix
    for(int i = 0; i < matrix.size(); ++i)
        for(int j = 0; j < matrix.get(0).size(); ++j)
         
            // Adding the element into the
            // defined matrix
            m.add(matrix.get(i).get(j));
 
    return m;
}
 
// Function to enlarge a matrix
// such that each element occurs
// in R rows and C columns
static ArrayList>dimensions(
    int R, int C, ArrayList matrix)
{
     
    // Initializing the enlarged
    // matrix
    ArrayList> ans = new ArrayList<>();
    for(int i = 0; i < R; ++i)
    {
        ans.add(new ArrayList());
    }
 
    // Variables to iterate over
    // the matrix
    int ctr = 0;
    int r = 0;
 
    while (ctr < matrix.size())
    {
         
        // To keep the rows in the
        // range [0, R]
        r = r % R;
        int c = 0;
         
        while (c < C)
        {
             
            // In order to fix the number
            // of columns, the above r
            // statement can be commented
            // and this can be uncommented
            // c = c % C
            for(int i = 0; i < R; ++i)
            {
                ans.get(i).add(matrix.get(ctr));
            }
            c += 1;
        }
        ctr += 1;
        r += 1;
    }
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
 
    ArrayList> arr = new ArrayList<>();
    arr.add(new ArrayList(Arrays.asList(1, 2)));
    arr.add(new ArrayList(Arrays.asList(3, 4)));
    int R = 2, C = 3;
 
    ArrayList> ans = new ArrayList<>(
        dimensions(R, C, oneD(arr)));
 
    // Print the enlarged matrix
    for(int i = 0; i < ans.size(); ++i)
    {
        System.out.print("[");
        for(int j = 0; j < ans.get(i).size(); ++j)
        {
            System.out.print(ans.get(i).get(j) + ", ");
        }
        System.out.print("]\n");
    }
}
}
 
// This code is contributed by akhilsaini


Python3
# Python program to enlarge a matrix
# such that each element occurs in
# R rows and C columns
 
# Function to convert the matrix
# into a 1-D array
def oneD(matrix):
     
    # Variable to store the
    # given matrix
    m = []
 
    # Iterating through all the
    # elements of the matrix
    for i in range(len(matrix)):
        for j in range(len(matrix[0])):
 
            # Adding the element into the
            # defined matrix
            m.append(matrix[i][j])
 
    return m[:]
 
# Function to enlarge a matrix
# such that each element occurs
# in R rows and C columns
def dimensions(R, C, matrix):
     
    # Initializing the enlarged
    # matrix
    ans = [[]] * R
     
    # Variables to iterate over
    # the matrix
    ctr = 0
    r = 0
 
     
    while(ctr < len(matrix)):
 
        # To keep the rows in the
        # range [0, R]
        r = r % R
        c = 0
        while(c < C):
             
            # In order to fix the number
            # of columns, the above r
            # statement can be commented 
            # and this can be uncommented
            # c = c % C
            ans[r].append(matrix[ctr])
             
            c+= 1
      
        ctr += 1
        r+= 1
         
         
    return ans
 
# Driver code
if __name__ == '__main__':
 
    arr = [[1, 2], [3, 4]]
    R, C = 2, 3
 
    ans = dimensions(R, C, oneD(arr))
     
    # Print the enlarged matrix
    for i in ans:
        print(i)


C#
// C# program to enlarge a matrix
// such that each element occurs in
// R rows and C columns
using System;
using System.Collections;
using System.Collections.Generic;
 
class GFG{
 
// Function to convert the matrix
// into a 1-D array
static List oneD(List > matrix)
{
     
    // Variable to store the
    // given matrix
    List m = new List();
 
    // Iterating through all the
    // elements of the matrix
    for(int i = 0; i < matrix.Count; ++i)
        for(int j = 0; j < matrix[0].Count; ++j)
         
            // Adding the element into the
            // defined matrix
            m.Add(matrix[i][j]);
 
    return m;
}
 
// Function to enlarge a matrix
// such that each element occurs
// in R rows and C columns
static List> dimensions(int R, int C,
                                  List matrix)
{
     
    // Initializing the enlarged
    // matrix
    List > ans = new List >();
    for(int i = 0; i < R; ++i)
    {
        ans.Add(new List());
    }
 
    // Variables to iterate over
    // the matrix
    int ctr = 0;
    int r = 0;
 
    while (ctr < matrix.Count)
    {
         
        // To keep the rows in the
        // range [0, R]
        r = r % R;
        int c = 0;
         
        while (c < C)
        {
             
            // In order to fix the number
            // of columns, the above r
            // statement can be commented
            // and this can be uncommented
            // c = c % C
            for(int i = 0; i < R; ++i)
            {
                ans[i].Add(matrix[ctr]);
            }
            c += 1;
        }
        ctr += 1;
        r += 1;
    }
    return ans;
}
 
// Driver code
public static void Main()
{
 
    List> arr = new List>();
    arr.Add(new List() { 1, 2 });
    arr.Add(new List() { 3, 4 });
    int R = 2, C = 3;
 
    List> ans = new List>(
        dimensions(R, C, oneD(arr)));
 
    // Print the enlarged matrix
    for(int i = 0; i < ans.Count; ++i)
    {
        Console.Write("[");
        for(int j = 0; j < ans[i].Count; ++j)
        {
            Console.Write(ans[i][j] + ", ");
        }
        Console.Write("]\n");
    }
}
}
 
// This code is contributed by akhilsaini


输出:
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]
[1, 1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 4]