码迷,mamicode.com
首页 > 其他好文 > 详细

蛇形矩阵构造

时间:2016-04-03 21:51:00      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

#include <iostream>
#define max 10000
using namespace std;

int G[max][max];

int main()
{
    int n;
    cin>>n;
    int count;
    if(n%2==0)count=n/2;
    else count=n/2+1;
    int ans = 1;
    for(int t = 0;t < count;t++){
        for(int i = t;i < n-t;i++)
            G[t][i]=ans++;
        for(int j = t+1;j < n-t;j++)
            G[j][n-1-t]=ans++;
        for(int i = n-2-t;i>=t;i--)
            G[n-1-t][i]=ans++;
        for(int j = n-2-t;j>t&&ans<n*n;j--)
            G[j][t]=ans++;
    }

    for(int i = 0;i < n;i++){
        for(int j = 0;j < n;j++){
            cout<<G[i][j]<<" ";
        }
    }
    return 0;
}

效果图:

技术分享

技术分享

敲代码还是要戒骄戒躁,一着急就啥都不行了,唉

蛇形矩阵构造

标签:

原文地址:http://www.cnblogs.com/tianxia2s/p/5350651.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!