close
題目概要:
判斷矩陣是否為對稱矩陣。Ex 有一個4x4矩陣,則(1,1)→(4,4)、(1,2)→(4,3)、(1,3)→(4,2)、(1,4)→(4,1)、(2,1)→(3,4)、(2,2)→(3,3)、(2,3)→(3,2)、(2,4)→(3,1)。 (此題對稱法跟一般印象中對稱矩陣不同)
解題方向:
把2維矩陣變成1維矩陣,判斷首尾數值是否相同。
程式碼:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Java | |
import java.util.Scanner; | |
class uva11349{ | |
public static void main(String args[]){ | |
Scanner sc=new Scanner(System.in); | |
int cases=Integer.parseInt(sc.next()); //總共多少Case。 | |
for(int i=0;i<cases;i++){ | |
String temp1=sc.next(),temp2=sc.next(); //進行假抓(N、=)。 | |
int n=Integer.parseInt(sc.next()); //矩陣大小。 | |
long arr[]=new long[n*n]; | |
int size=n*n; | |
for(int j=0;j<size;j++) arr[j]=Long.parseLong(sc.next()); //把數值讀到矩陣。 | |
//進行判斷。 | |
boolean flag=true; | |
for(int j=0;j<size;j++){ | |
if(arr[j]<0 || (arr[j]!=arr[size-1-j])){ | |
flag=false; | |
break; | |
} | |
} | |
//結果輸出。 | |
if(flag) System.out.println("Test #"+(i+1)+": Symmetric."); | |
else System.out.println("Test #"+(i+1)+": Non-symmetric."); | |
} | |
} | |
} |
文章標籤
全站熱搜