打印一个 N x M 矩阵,使得每一行和每一列都包含所有元音
给定两个整数N和M ,任务是打印一个N x M矩阵,使得每一行和每一列都包含其中的所有元音。如果不可能这样做,则打印-1 。
例子:
Input: N = 5, M = 5
Output:
a e i o u
e i o u a
i o u a e
o u a e i
u a e i o
Input: N = 6, M = 2
Output: -1
方法:由于元音的数量为 5,因此我们需要至少 5 行和 5 列才能生成有效矩阵。一个模式可以在第一行填充“aeiouaeiou..” ,第二行填充“eiouaeio..”,依此类推,生成的矩阵将包含每一行和每一列中的所有元音。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to print the required matrix
void printMatrix(int n, int m)
{
// Impossible to generate
// the required matrix
if (n < 5 || m < 5) {
cout << -1;
return;
}
// Store all the vowels
string s = "aeiou";
// Print the matrix
for (int i = 0; i < n; i++) {
// Print vowels for every index
for (int j = 0; j < m; j++) {
cout << s[j % 5] << " ";
}
cout << endl;
char c = s[0];
// Shift the vowels by one
for (int i = 0; i < 4; i++) {
s[i] = s[i + 1];
}
s[4] = c;
}
}
// Driver code
int main()
{
int n = 5, m = 5;
printMatrix(n, m);
return 0;
}
Java
// Java implementation of the approach
class GFG
{
// Function to print the required matrix
static void printMatrix(int n, int m)
{
// Impossible to generate
// the required matrix
if (n < 5 || m < 5)
{
System.out.print(-1);
return;
}
// Store all the vowels
char[] s = "aeiou".toCharArray();
// Print the matrix
for (int i = 0; i < n; i++)
{
// Print vowels for every index
for (int j = 0; j < m; j++)
{
System.out.print(s[j % 5] + " ");
}
System.out.println();
char c = s[0];
// Shift the vowels by one
for (int k = 0; k < 4; k++)
{
s[k] = s[k + 1];
}
s[4] = c;
}
}
// Driver code
public static void main(String[] args)
{
int n = 5, m = 5;
printMatrix(n, m);
}
}
// This code has been contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
# Function to print the required matrix
def printMatrix(n, m) :
# Impossible to generate
# the required matrix
if (n < 5 or m < 5) :
print(-1,end = " ");
return;
# Store all the vowels
s = "aeiou";
s = list(s);
# Print the matrix
for i in range(n) :
# Print vowels for every index
for j in range(m) :
print(s[j % 5],end= " ");
print()
c = s[0];
# Shift the vowels by one
for i in range(4) :
s[i] = s[i + 1];
s[4] = c;
# Driver code
if __name__ == "__main__" :
n = 5; m = 5;
printMatrix(n, m);
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to print the required matrix
static void printMatrix(int n, int m)
{
// Impossible to generate
// the required matrix
if (n < 5 || m < 5)
{
Console.Write(-1);
return;
}
// Store all the vowels
char[] s = "aeiou".ToCharArray();
// Print the matrix
for (int i = 0; i < n; i++)
{
// Print vowels for every index
for (int j = 0; j < m; j++)
{
Console.Write(s[j % 5] + " ");
}
Console.WriteLine();
char c = s[0];
// Shift the vowels by one
for (int k = 0; k < 4; k++)
{
s[k] = s[k + 1];
}
s[4] = c;
}
}
// Driver code
public static void Main(String[] args)
{
int n = 5, m = 5;
printMatrix(n, m);
}
}
/* This code contributed by PrinciRaj1992 */
PHP
Javascript
输出:
a e i o u
e i o u a
i o u a e
o u a e i
u a e i o