填充幻方的缺失条目
给定一个 3X3 矩阵mat ,它的左对角元素丢失(设置为0 ),考虑到原始矩阵的每行、每列和对角线的总和相等,任务是找到丢失的对角元素并打印原始矩阵。
例子:
Input: mat[][] = {{0, 7, 6}, {9, 0, 1}, {4, 3, 0}}
Output:
2 7 6
9 5 1
4 3 8
Row sum = Column sum = Diagonal sum = 15
Input: mat[][] = {{0, 1, 1}, {1, 0, 1}, {1, 1, 0}}
Output:
1 1 1
1 1 1
1 1 1
方法:让Sum表示不包括对角线元素的总和,
Sum = total sum of the given matrix – diagonalSum
Sum = (3 * rowSum) – diagonalSum
Sum = (2 * rowSum) [Since, columnSum = rowSum = diagonalSum]
rowSum = Sum / 2
因此,我们可以在每一行中插入一个元素,使得该行的总和为rowSum
下面是上述方法的实现:
C++
// C++ program to fill blanks with numbers
#include
using namespace std;
// Function to print the original matrix
int printFilledDiagonal(int sq[][3])
{
// Calculate the sum of all the elements
// of the matrix
int sum = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
sum += sq[i][j];
// Required sum of each row (from the approach)
sum /= 2;
for (int i = 0; i < 3; i++) {
// Row sum excluding the diagonal element
int rowSum = 0;
for (int j = 0; j < 3; j++)
rowSum += sq[i][j];
// Element that must be inserted at
// diagonal element of the current row
sq[i][i] = sum - rowSum;
}
// Print the updated matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
cout << sq[i][j] << " ";
cout << endl;
}
}
// Driver Program to test above function
int main()
{
int sq[3][3] = {
{ 0, 7, 6 },
{ 9, 0, 1 },
{ 4, 3, 0 }
};
printFilledDiagonal(sq);
return 0;
}
Java
// Java program to fill blanks with numbers
import java.io.*;
class GFG {
// Function to print the original matrix
static int printFilledDiagonal(int sq[][])
{
// Calculate the sum of all the elements
// of the matrix
int sum = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
sum += sq[i][j];
// Required sum of each row (from the approach)
sum /= 2;
for (int i = 0; i < 3; i++) {
// Row sum excluding the diagonal element
int rowSum = 0;
for (int j = 0; j < 3; j++)
rowSum += sq[i][j];
// Element that must be inserted at
// diagonal element of the current row
sq[i][i] = sum - rowSum;
}
// Print the updated matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
System.out.print( sq[i][j] + " ");
System.out.println();
}
return 0;
}
// Driver Program to test above function
public static void main (String[] args) {
int sq[][] = {
{ 0, 7, 6 },
{ 9, 0, 1 },
{ 4, 3, 0 }
};
printFilledDiagonal(sq);
}
}
// This code is contributed by anuj_67..
Python3
# Python3 program to fill blanks
# with numbers
# Function to print the original matrix
def printFilledDiagonal(sq):
# Calculate the sum of all the
# elements of the matrix
Sum = 0
for i in range(0, 3):
for j in range(0, 3):
Sum += sq[i][j]
# Required sum of each
# row (from the approach)
Sum = Sum//2
for i in range(0, 3):
# Row sum excluding the
# diagonal element
rowSum = 0
for j in range(0, 3):
rowSum += sq[i][j]
# Element that must be inserted
# at diagonal element of the
# current row
sq[i][i] = Sum - rowSum
# Print the updated matrix
for i in range(0, 3):
for j in range(0, 3):
print(sq[i][j], end = " ")
print()
# Driver Code
if __name__ == "__main__":
sq = [[0, 7, 6],
[9, 0, 1],
[4, 3, 0]]
printFilledDiagonal(sq)
# This code is contributed
# by Rituraj Jain
C#
// C# program to fill blanks with numbers
using System;
class GFG {
// Function to print the original matrix
static int printFilledDiagonal(int [,]sq)
{
// Calculate the sum of all the elements
// of the matrix
int sum = 0;
for (int i = 0; i < 3; i++)
for (int j = 0; j < 3; j++)
sum += sq[i,j];
// Required sum of each row (from the approach)
sum /= 2;
for (int i = 0; i < 3; i++) {
// Row sum excluding the diagonal element
int rowSum = 0;
for (int j = 0; j < 3; j++)
rowSum += sq[i,j];
// Element that must be inserted at
// diagonal element of the current row
sq[i,i] = sum - rowSum;
}
// Print the updated matrix
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++)
Console.Write( sq[i,j] + " ");
Console.WriteLine();
}
return 0;
}
// Driver Program to test above function
public static void Main () {
int [,]sq = {
{ 0, 7, 6 },
{ 9, 0, 1 },
{ 4, 3, 0 }
};
printFilledDiagonal(sq);
}
}
// This code is contributed by inder_verma
PHP
Javascript
输出:
2 7 6
9 5 1
4 3 8
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。