每值为0或n的矩阵的最大行列式

我们已经给出了一个正数n,我们必须找到一个3*3矩阵,它可以由0或n的组合构成,并且具有最大行列式。

null

例如:

Input : n = 3 Output : Maximum determinant = 54Resultant Matrix :3 3 00 3 33 0 3Input : n = 13 Output : Maximum determinant = 4394Resultant Matrix :13 13  00  13 1313  0 13

说明: 对于元素为0或n的任何3*3矩阵,最大可能行列式为 2*(n^3)。 .此外,具有最大行列式的矩阵的形式为: n n 0 0 n n n 0 0

C++

// C++ program to find  maximum possible determinant
// of 0/n matrix.
#include <bits/stdc++.h>
using namespace std;
// Function for maximum determinant
int maxDet( int n)
{
return (2*n*n*n);
}
// Function to print resultant matrix
void resMatrix ( int n)
{
for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 3; j++)
{
// three position where 0 appears
if (i == 0 && j == 2)
cout << "0 " ;
else if (i == 1 && j == 0)
cout << "0 " ;
else if (i == 2 && j == 1)
cout << "0 " ;
// position where n appears
else
cout << n << " " ;
}
cout << "" ;
}
}
// Driver code
int main()
{
int n = 15;
cout << "Maximum Determinant = " << maxDet(n);
cout << "Resultant Matrix :" ;
resMatrix(n);
return 0;
}


JAVA

// Java program to find maximum possible
// determinant of 0/n matrix.
import java.io.*;
public class GFG
{
// Function for maximum determinant
static int maxDet( int n)
{
return ( 2 * n * n * n);
}
// Function to print resultant matrix
void resMatrix( int n)
{
for ( int i = 0 ; i < 3 ; i++)
{
for ( int j = 0 ; j < 3 ; j++)
{
// three position where 0 appears
if (i == 0 && j == 2 )
System.out.print( "0 " );
else if (i == 1 && j == 0 )
System.out.print( "0 " );
else if (i == 2 && j == 1 )
System.out.print( "0 " );
// position where n appears
else
System.out.print(n + " " );
}
System.out.println( "" );
}
}
// Driver code
static public void main (String[] args)
{
int n = 15 ;
GFG geeks= new GFG();
System.out.println( "Maximum Determinant = "
+ maxDet(n));
System.out.println( "Resultant Matrix :" );
geeks.resMatrix(n);
}
}
// This code is contributed by vt_m.


Python3

# Python 3 program to find maximum
# possible determinant of 0/n matrix.
# Function for maximum determinant
def maxDet(n):
return 2 * n * n * n
# Function to print resultant matrix
def resMatrix(n):
for i in range ( 3 ):
for j in range ( 3 ):
# three position where 0 appears
if i = = 0 and j = = 2 :
print ( "0" , end = " " )
elif i = = 1 and j = = 0 :
print ( "0" , end = " " )
elif i = = 2 and j = = 1 :
print ( "0" , end = " " )
# position where n appears
else :
print (n, end = " " )
print ( "" )
# Driver code
n = 15
print ( "Maximum Detrminat=" , maxDet(n))
print ( "Resultant Matrix:" )
resMatrix(n)
# This code is contributed by Shrikant13


C#

// C# program to find maximum possible
// determinant of 0/n matrix.
using System;
public class GFG
{
// Function for maximum determinant
static int maxDet( int n)
{
return (2 * n * n * n);
}
// Function to print resultant matrix
void resMatrix( int n)
{
for ( int i = 0; i < 3; i++)
{
for ( int j = 0; j < 3; j++)
{
// three position where 0 appears
if (i == 0 && j == 2)
Console.Write( "0 " );
else if (i == 1 && j == 0)
Console.Write( "0 " );
else if (i == 2 && j == 1)
Console.Write( "0 " );
// position where n appears
else
Console.Write(n + " " );
}
Console.WriteLine( "" );
}
}
// Driver code
static public void Main (String []args)
{
int n = 15;
GFG geeks= new GFG();
Console.WriteLine( "Maximum Determinant = "
+ maxDet(n));
Console.WriteLine( "Resultant Matrix :" );
geeks.resMatrix(n);
}
}
// This code is contributed by vt_m.


PHP

<?php
// PHP program to find maximum
// possible determinant of 0/n matrix.
// Function for maximum determinant
function maxDet( $n )
{
return (2 * $n * $n * $n );
}
// Function to print
// resultant matrix
function resMatrix ( $n )
{
for ( $i = 0; $i < 3; $i ++)
{
for ( $j = 0; $j < 3; $j ++)
{
// three position
// where 0 appears
if ( $i == 0 && $j == 2)
echo "0 " ;
else if ( $i == 1 && $j == 0)
echo "0 " ;
else if ( $i == 2 && $j == 1)
echo "0 " ;
// position where n appears
else
echo $n , " " ;
}
echo "" ;
}
}
// Driver code
$n = 15;
echo "Maximum Determinant = " ,
maxDet( $n );
echo "Resultant Matrix :" ;
resMatrix( $n );
// This code is contributed
// by nitin mittal.
?>


Javascript

<script>
// Java script program to find maximum possible
// determinant of 0/n matrix.
// Function for maximum determinant
function maxDet(n)
{
return (2 * n * n * n);
}
// Function to print resultant matrix
function resMatrix(n)
{
for (let i = 0; i < 3; i++)
{
for (let j = 0; j < 3; j++)
{
// Three position where 0 appears
if (i == 0 && j == 2)
document.write( "0 " );
else if (i == 1 && j == 0)
document.write( "0 " );
else if (i == 2 && j == 1)
document.write( "0 " );
// Position where n appears
else
document.write(n + " " );
}
document.write( "<br>" );
}
}
// Driver code
let n = 15;
document.write( "Maximum Determinant = " +
maxDet(n) + "<br>" );
document.write( "Resultant Matrix :<br>" );
resMatrix(n);
// This code is contributed by sravan kumar
</script>


输出:

Maximum Determinant = 6750Resultant Matrix :15 15  00  15 1515 0  15

练习: 将上述解推广到广义k×k矩阵。

本文由 希瓦姆·普拉丹(anuj_charm) .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享