广告广告
  加入我的最爱 设为首页 风格修改
首页 首尾
 手机版   订阅   地图  繁体 
您是第 3573 个阅读者
 
发表文章 发表投票 回覆文章
  可列印版   加为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.020676 second(s),query:16 Gzip disabled
本站由 瀛睿律师事务所 担任常年法律顾问 | 免责声明 | 本网站已依台湾网站内容分级规定处理 | 连络我们 | 访客留言