廣告廣告
  加入我的最愛 設為首頁 風格修改
首頁 首尾
 手機版   訂閱   地圖  簡體 
您是第 3548 個閱讀者
 
發表文章 發表投票 回覆文章
  可列印版   加為IE收藏   收藏主題   上一主題 | 下一主題   
f80731
個人文章 個人相簿 個人日記 個人地圖
路人甲
級別: 路人甲 該用戶目前不上站
推文 x0 鮮花 x15
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片
推文 x0
[C/C++][求助] 高手救救我阿
依逆時針將數字填滿一個N×N矩陣

請寫一個程式,執行時先輸入一個小於10的數目N, ..

訪客只能看到部份內容,免費 加入會員 或由臉書 Google 可以看到全部內容



         
獻花 x0 回到頂端 [樓 主] From:台灣中華電信 | Posted:2006-01-01 23:40 |
wusun
數位造型
個人文章 個人相簿 個人日記 個人地圖
小人物
級別: 小人物 該用戶目前不上站
推文 x0 鮮花 x11
分享: 轉寄此文章 Facebook Plurk Twitter 複製連結到剪貼簿 轉換為繁體 轉換為簡體 載入圖片

Compiler:VC++

複製程式
#if 0
1.假設以左下角為原點。
2.X軸向右為正。
3.Y軸向上為正。
#endif


#include <iostream>

using namespace std;

const int DX[4] = {0, -1,  0, 1};
const int DY[4] = {1,  0, -1, 0};

class CIntMartix
{
private:
    int m_N;
    int **m_ptr;

public:
    CIntMartix(int N)
    {
        m_N = N;
        m_ptr = new int *[m_N];
        for(int i = 0; i < m_N; i++)
        {
            m_ptr[i] = new int[m_N];
            for(int j = 0; j < N; j++)
                m_ptr[i][j] = 0;
        }

    }

    ~CIntMartix()
    {
        for(int i = 0; i < m_N; i++)
            delete [] m_ptr[i];
    }

    void Display(void)
    {
        int x, y;

        for(y = m_N - 1; y >= 0; y--)
        {
            for(x = 0; x < m_N; x++)
                cout << m_ptr[x][y] << "\t";
            cout << endl;
        }
    }

    int & Get(int x, int y)
    {
        return m_ptr[x][y];
    }
};

int main(void)
{
    int N;

    cout << "Please input N:";
    cin >> N;

    if (N < 2 || N > 10)
    {
        cout << "N must be between 2 to 10." << endl;
        return 1;
    }

    CIntMartix MyMatrix(N);
    int iCounter = 1;
    int x = N - 1;
    int y = 0;
    int dir = 0;

    //Initial the first point.
    MyMatrix.Get(x, y) = 1;
    iCounter++;

    while(iCounter <= N * N) 
    {
        int x1 = x + DX[dir];
        int y1 = y + DY[dir];
        if(x1 >= 0 && y1 < N &&
           y1 >= 0 && y1 < N &&
           MyMatrix.Get(x1, y1) == 0)
        {
            x = x1;
            y = y1;
            MyMatrix.Get(x, y) = iCounter;
            iCounter++;
        }
        else
        {   //Turn to next direction.
            dir++;
            if(dir == 4)
                dir = 0;
        }
    }

    MyMatrix.Display();

    //system("pause");
    return 0;
}


獻花 x0 回到頂端 [1 樓] From:台灣數位聯合 | Posted:2006-01-03 18:56 |

首頁  發表文章 發表投票 回覆文章
Powered by PHPWind v1.3.6
Copyright © 2003-04 PHPWind
Processed in 0.018731 second(s),query:16 Gzip disabled
本站由 瀛睿律師事務所 擔任常年法律顧問 | 免責聲明 | 本網站已依台灣網站內容分級規定處理 | 連絡我們 | 訪客留言