顺时针输出二维数组元素值

参考 http://blog.csdn.net/da1954ping/article/details/8043815


代码

#include <iostream>

using namespace std;

//顺时针输出二维数组
void printMatrix(int a[][5],int row)
{
    //cout << "size of a ="<< sizeof(a)<<endl;
    //cout << "size of a[0] =" << sizeof(a[0]) <<endl;
    //列数,第二维度长度
    //cout << sizeof(a[0])/sizeof(int) << endl;
    int left=0;
    int column=sizeof(a[0])/sizeof(int);

    //行数,第一维长度
    //cout << sizeof(a)/sizeof(a[0]) <<"-----" << endl;
    int low=0;
    //int row=sizeof(a)/sizeof(a[0]);


    while(left<column && low<row)
    {
        for(int i=left;i<column;i++)
        {
            cout << a[low][i] << " ";
        }
        low++;

        for(int j=low;j<row;j++)
        {
            cout << a[j][column-1] << " ";
        }
        column--;

        for(int i=column;i>left;i--)
        {
            cout << a[row-1][i-1] << " ";
        }
        row--;

        for(int j=row;j>low;j--) {
            cout << a[j-1][left] << " ";
        }
        left++;

    }
    cout << endl;
}

int main()
{
    int a[4][5]={
        {1,  2, 3, 4, 5},
        {14,15,16,17, 6},
        {13,20,19,18, 7},
        {12,11,10, 9, 8}
    };
    printMatrix(a,4);
    //  a   80
    //  a[0] 20
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值