posts - 71,  comments - 41,  trackbacks - 0
output1.JPG
Just like this

#include <cstdio>
void MatrixSpiralOutput(int n)
{
    
int **matrix = new int*[n]();
    
for (int idx = 0; idx < n; idx++)
    
{
        matrix[idx] 
= new int[n]();
    }


    
int row = 0, col = 0;
    
int i = 0;
    
int len = n * n;
    
int circle = 0;

    
while (i < len)
    
{
        
for( ;row < n - circle; row++)
            matrix[row][col] 
= ++i;

        row
--;
        col
++;

        
for( ; col < n - circle; col++
            matrix[row][col] 
= ++i;

        row
--;
        col
--;

        
for( ; row >= circle; row--
            matrix[row][col] 
= ++i;

        row
++;
        col
--;

        
for( ;col > circle; col--
            matrix[row][col] 
= ++i;

        row
++;
        col
++;

        circle
++;
    }


    printf(
"\n   The  Array matrix[%d][%d] is :", n, n);
    
for(int k = 0; k < n; k++)
    
{
        printf(
"\n\n      ");
        
for(int j = 0; j < n; j++)
            printf(
"%-5d", matrix[k][j]);
    }

    printf(
"\n\n");

    
for(int idx = 0 ; idx < n ; idx++)
        delete [] matrix[idx];
    delete [] matrix;

}
posted on 2006-12-04 10:08 Charles 阅读(1192) 评论(0)  编辑 收藏 引用 所属分类: 面试小算法

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2006年12月>
262728293012
3456789
10111213141516
17181920212223
24252627282930
31123456

决定开始写工作日记,记录一下自己的轨迹...

常用链接

留言簿(4)

随笔分类(70)

随笔档案(71)

charles推荐访问

搜索

  •  

积分与排名

  • 积分 - 48113
  • 排名 - 455

最新评论

阅读排行榜

评论排行榜