循环输出

#include <iostream>
#include <cstdio>
using namespace std;
void CirPrint( int *number,int n,int m)
{
    if(number==NULL) return;
    int start=0;
    int cout=0;
    while(cout<n*m)
    {
        for(int i=start;i<n-start&&cout<n*m;i++)
        {
            printf("%3d",number[start*n+i]);
            cout++;
        }
        for(int i=start+1;i<m-start&&cout<n*m;i++)
        {
            printf("%3d",number[i*n+(n-1-start)]);
            cout++;
        }

        for(int i=n-2-start;i>=start&&cout<n*m;i--)
        {
            printf("%3d",number[(m-1-start)*n+i]);
            cout++;
        }

        for(int i=m-2-start;i>start&&cout<n*m;i--)
        {
            printf("%3d",number[i*n+start]);
            cout++;
        }
        start++;
    }
}

int main()
{
    int *number;
    int n,m;
    scanf("%d %d",&n,&m);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<m;j++)
        scanf("%d",&(number[i*m+j]));
    }
    CirPrint(number,n,m);
    //cout << "Hello world!" << endl;
    return 0;
}

posted on 2014-07-03 13:13  XiaoFei Wang  阅读(138)  评论(0编辑  收藏  举报

导航