2016年10月16日 星期日

ITSA50 Problem2 The Typhoon

Problem 2. The Typhoon 
(Time Limit: 2 seconds) 

問題描述 : 
已知行列式運算為 
a b c
d e f
g h i 
= a * e * i + d * h * c + g * b * f - c * e * g - b * d * i - a * f * h 

求以下三階行列式的結果。 

輸入說明: 
第一行輸入一正整數 N,表示共有N 筆測試資料(1 ≤ N ≤ 100),每筆測試資
料為一個行列式,行列式內容為 3 列,每一列有 3 個整數,用空白分開。 
例如輸入內容為: 
1 2 3 
4 5 6 
7 8 9 
輸出說明: 
輸出計算的答案,最後必須有換行字元。 
例如輸出為:0 

範例: 
Sample Input:       Sample Output: 
2                            0
1 2 3                      -258
4 5 6 
7 8 9 
6 9 4 
2 4 9 
1 8 3 

重點說明:阿上面都跟我說了,直接寫了


程式碼:
import java.util.Scanner;
public class ITSA50_2 {

public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a,b,c,d,e,f,g,h,i;
while(n-->0){
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
d=sc.nextInt();
e=sc.nextInt();
f=sc.nextInt();
g=sc.nextInt();
h=sc.nextInt();
i=sc.nextInt();
System.out.println( a * e * i + d * h * c + g * b * f - c * e * g - b * d * i - a * f * h );
}
}
}

沒有留言:

張貼留言