蛇形矩阵的各种情形:共4种5类

/*
  Name: 
  Copyright: goal00001111
  Author: 
  Date: 14-11-08 09:25
  Description: 蛇形矩阵的各种情形:共4种5类 
*/
#include<iostream>

using namespace std;

const int MAX = 20;

int a[MAX][MAX];

void Fun_1(int n);
void Fun_2(int n);
void Fun_3(int n);
void Fun_4(int n);
void Fun_5(int n);

int main(void)
{
   int n;
   cout << "请输入矩阵大小:" << endl;
   cin >> n;
   
   Fun_1(n);
   cout << endl;
   Fun_2(n); 
   cout << endl;
   Fun_3(n); 
   cout << endl;
   Fun_4(n); 
   cout << endl;
   Fun_5(n); 
 
   system("pause"); 
   return 0;
}

void Fun_1(int n)
{
    int s = 1;
    for (int i=0; i<n; i++)
    {
        if (i % 2 == 0)
        {
            for (int j=0; j<n; j++)
                a[i][j] = s++;
        }
        else
        {
            for (int j=n-1; j>=0; j--)
                a[i][j] = s++;
        }
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
            printf("%3.0d ",a[i][j]);
        cout << endl;
    }
}

void Fun_2(int n)
{
    int i, j, s;
    i = j = 0;
    s = 1;
    a[i][j] = s++;
    while (s <= n*n)
    {
        //下移 
        if (i < n-1)
            i++;
        else
            j++;
        a[i][j] = s++;
        //右上移动
        while (j < n-1 && i > 0) 
            a[--i][++j] = s++;
        //右移
        if (j < n-1)
            j++;
        else
            i++;
        a[i][j] = s++; 
        //左下移动
        while (i < n-1 && j > 0) 
            a[++i][--j] = s++; 
    } 
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
            printf("%3.0d ",a[i][j]);
        cout << endl;
    }
}


void Fun_3(int n)
{
    int left, right, up, down;
    left = up = 0;
    right = down = n - 1;
    int s = 1;
    while (left <= right)
    {
        for (int i=left; i<=right; i++)//顺序横向 
            a[up][i] = s++;
        up++; //下移一行 
        for (int i=up; i<=down; i++)//顺序纵向 
            a[i][right] = s++;
        right--; //左移一列 
        for (int i=right; i>=left; i--)//顺序横向 
            a[down][i] = s++;
        down--; //上移一行 
        for (int i=down; i>=up; i--)//顺序纵向 
            a[i][left] = s++;
        left++;
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
            printf("%3.0d ",a[i][j]);
        cout << endl;
    }
}

void Fun_4(int n)
{
    int left, right, up, down;
    left = up = 0;
    right = down = n - 1;
    int s = 1;
    while (left <= right)
    {
        for (int i=left; i<=right; i++)//顺序横向 
            a[up][i] = s++;
        up++; //下移一行 
        for (int i=up; i<=down; i++)//顺序纵向 
            a[i][right] = s++;
        right--; //左移一列 
        for (int i=right; i>=left; i--)//顺序横向 
            a[down][i] = s++;
        down--; //上移一行 
        for (int i=down; i>=up; i--)//顺序纵向 
            a[i][left] = s++;
        left++;
    }
    for (int i=0; i<n; i++)
    {
        for (int j=0; j<n; j++)
            printf("%3.0d ",n*n + 1 - a[i][j]);
        cout << endl;
    }
}

void Fun_5(int n)
{
    for (int s=1; s<=n; s++)
    {   //列 
        for (int i=s-1; i<n+n-s; i++)//s<n/2 
            a[i][s-1] = a[i][n+n-s-1] = s;
        //行 
        for (int i=s-1; i<n+n-s; i++)//s<n/2 
            a[s-1][i] = a[n+n-s-1][i] = s;
    }
    for (int i=0; i<n+n-1; i++)
    {
        for (int j=0; j<n+n-1; j++)
            printf("%3.0d ", a[i][j]);
        cout << endl;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值