Created
June 10, 2014 13:05
-
-
Save HabaCo/413de43a49c95618b3a4 to your computer and use it in GitHub Desktop.
讀檔&矩陣相加
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
package Quiz; | |
import java.io.BufferedReader; | |
import java.io.FileReader; | |
import java.io.IOException; | |
// 2矩陣相加 (等邊的方矩陣) | |
public class Q940305 { | |
public static void main(String[] args) throws IOException{ | |
String fileName = "940305.SM"; | |
FileReader fr = new FileReader(fileName); | |
BufferedReader br = new BufferedReader(fr); | |
// 先做一次 readLine(),透過數列個數取得矩陣的大小 (邊長n) | |
String s = br.readLine(); | |
int i=0; | |
String[] tmp = s.split(", "); | |
int n = tmp.length; | |
int[][] arr = new int[n*2][n]; // 定義 2 個矩陣的空間 | |
for (int j=0; j<n; j++) | |
arr[i][j] = Integer.parseInt(tmp[j]); | |
i++; | |
// 取得2矩陣資料 | |
while ((s=br.readLine())!=null && s.length()>0){ | |
tmp = s.split(", "); | |
for (int j=0; j<n; j++) | |
arr[i][j] = Integer.parseInt(tmp[j]); | |
i++; | |
} | |
// 矩陣相加並印出 | |
for (i=0; i<n; i++){ | |
for (int j=0; j<n; j++){ | |
arr[i][j]+=arr[i+n][j]; | |
System.out.print(arr[i][j]+" "); | |
} | |
System.out.println(); | |
} | |
fr.close(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment