Dado un número N denota el número total de elementos en una array, la tarea es imprimir todo el orden posible de la array. Un orden es un par (m, n) de números enteros donde m es el número de filas y n es el número de columnas. Por ejemplo, si el número de elementos es 8, todos los órdenes posibles son:
(1, 8), (2, 4), (4, 2), (8, 1).
Ejemplos:
Entrada: N = 8
Salida: (1, 2) (2, 4) (4, 2) (8, 1)
Entrada: N = 100
Salida:
(1, 100) (2, 50) (4, 25) ( 5, 20) (10, 10) (20, 5) (25, 4) (50, 2) (100, 1)
Enfoque:
Se dice que una array es de orden mxn si tiene m filas y n columnas. El número total de elementos en una array es igual a (m*n). Entonces comenzamos desde 1 y verificamos uno por uno si divide N (el número total de elementos). Si se divide, será un orden posible.
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <iostream> using namespace std; // Function to print all possible order void printAllOrder(int n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for (int i = 1; i <= n; i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if (n % i == 0) { // print the given format cout << i << " " << n / i << endl; } } } // Driver code int main() { int n = 10; printAllOrder(n); return 0; }
Java
// Java implementation of the above approach class GFG { // Function to print all possible order static void printAllOrder(int n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for (int i = 1; i <= n; i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if (n % i == 0) { // print the given format System.out.println( i + " " + n / i ); } } } // Driver code public static void main(String []args) { int n = 10; printAllOrder(n); } } // This code is contributed by ihritik
Python
# Python implementation of the above approach # Function to print all possible order def printAllOrder(n): # total number of elements in a matrix # of order m * n is equal (m*n) # where m is number of rows and n is # number of columns for i in range(1,n+1): # if n is divisible by i then i # and n/i will be the one # possible order of the matrix if (n % i == 0) : # print the given format print( i ,n // i ) # Driver code n = 10 printAllOrder(n) # This code is contributed by ihritik
C#
// C# implementation of the above approach using System; class GFG { // Function to print all possible order static void printAllOrder(int n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for (int i = 1; i <= n; i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if (n % i == 0) { // print the given format Console.WriteLine( i + " " + n / i ); } } } // Driver code public static void Main() { int n = 10; printAllOrder(n); } } // This code is contributed by ihritik
PHP
<?php // PHP implementation of the above approach // Function to print all possible order function printAllOrder($n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for ($i = 1; $i <= $n; $i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if ($n % $i == 0) { // print the given format echo $i, " ", ($n / $i), "\n"; } } } // Driver code $n = 10; printAllOrder($n); // This code is contributed by Ryuga ?>
Javascript
<script> // Java Script implementation of the above approach // Function to print all possible order function printAllOrder( n) { // total number of elements in a matrix // of order m * n is equal (m*n) // where m is number of rows and n is // number of columns for (let i = 1; i <= n; i++) { // if n is divisible by i then i // and n/i will be the one // possible order of the matrix if (n % i == 0) { // print the given format document.write( i + " " + n / i+"<br>" ); } } } // Driver code let n = 10; printAllOrder(n); // This code is contributed by sravan </script>
1 10 2 5 5 2 10 1
Complejidad de tiempo: O(n)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por Vivek.Pandit y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA