Home > Back-end >  Determine whether a N order integer matrix is symmetric matrix. Asked to matrix by one-dimensional a
Determine whether a N order integer matrix is symmetric matrix. Asked to matrix by one-dimensional a

Time:10-02

title content:

Determine whether a N order integer matrix is symmetric matrix, the matrix were asked to use a one-dimensional array to store,

input format:

N + 1 line, line 1: the number N (10 & gt; N> 0); Line 2 - (N + 1) : N order square element

output format:

"Yes" or "No"

input sample:

4

5 6 7 9

2 August 5 4

3 July 16 15

1 4 8 11

Output sample:

No
 
#include
using namespace std;

Bool f (int a [], int n) {
int i, j;
Int flag=1;//is used as the marker under the diagonal elements of a
Int count;//used to compare
for( i=0; iCount=1;
For (j=I + flag; jIf (a [j]. [j + count=a * (n - 1)]) return false.//as long as find a not equal to no longer continue to
Count + +;
}
Flag + +;
}
return true;
}
Int main () {
int a[100];
int n;
cin> n;
for(int i=0; i A [i++]);//in accordance with the line of the input matrix
//for (int I=0; iIf (f (a, n)) cout<& lt;" Yes ";
The else cout<& lt;" No ";

return 0;
}


algorithm analysis:
Subject requires the use of a one-dimensional array to store, the key is to find the elements of the plane, do not repeat comparison, with 4 order matrix a [16], for example, from a [0] to [15] a total of 16 elements, by the properties of similar matrices is a diagonal elements [0], a [5], a [10], a [15] don't need to compare, matrix n line the last line also do not need to compare, choose diagonal right here on [on the left side of the comparison is a [1] and a [4] is a [2] and a [8]... to pass out in each line from the first on the right side of the diagonal elements of a [j] should be a [j] + n - 1, the second element is a [j + 1] and a [j + 1 + 2 * (n - 1)], and so on,

CodePudding user response:

What do you want to ask
  • Related