發表於 C++CPE考古題UVA一顆星

【CPE】UVA 11349 Symmetric Matrix 20190924 考古題 by C++

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;
}


UVA 11349 CPE 20190924 考古題 第一題題目

作者:

一位 熱愛資工領域、喜歡好笑事物、偶爾打打網球 的學生 ! For A Better Me!

發表迴響

Please log in using one of these methods to post your comment:

WordPress.com 標誌

您的留言將使用 WordPress.com 帳號。 登出 /  變更 )

Facebook照片

您的留言將使用 Facebook 帳號。 登出 /  變更 )

連結到 %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.