11349 Symmetric Matrix
簡易翻譯和條件:
這題簡單來說就是要你找到輸入的測資是否是 Symmetric Matrix
至於什麼是 Symmetric Matrix 呢?!
題目中提到定義為:
- 必定是對稱矩陣
- 矩陣中元素不可為負數
題目就是輸出是否為 Symmetric Matrix
寫起來就是單純考驗對C++解題有沒有一點一般的sense吧!
我這題錯4次的原因就單純是因為沒看到第二個定義…..
矩陣中元素不可為負數!!
作法當然是用 stringstream 處理輸入格式
然後用一個 for 迴圈檢查前後對應的元素是否相同
矩陣中的元素我是使用 long long int ,不過其實仔細想一下我發現用 string 也可以啦,不過判斷的方法就要用字串的判斷方法囉~
如果有任何問題或是你有更好的寫法歡迎再下面留言討論喔!
我們下一題見啦!
程式碼:
#include #include #include #include using namespace std; int main() { int N = 0; cin >> N; int ORIGINN = N; cin.ignore(); stringstream ss; while(N--){ string line; getline(cin, line); ss.clear(); ss <> none; ss >> none; // 去掉 "W = " ss >> W; vector arr; for(int i=0; i<W; i++){ getline(cin, line); ss.clear(); ss << line; for(int x=0; x> temp; arr.push_back(temp); } } bool isGood = false; for(int x=0;x<=(W*W)/2; x++){ if(arr[x] < 0 || arr[W*W-x-1] < 0){ isGood = false; break; } else if(arr[x]==arr[W*W-x-1]){ isGood = true; continue; }else{ isGood = false; break; } } if(isGood) cout << "Test #" << ORIGINN-N << ": Symmetric." << endl; else cout << "Test #" << ORIGINN-N << ": Non-symmetric." << endl; } return 0; }
