Snail in Matrix[矩阵的螺旋输出][顺时针螺旋输出]

4 篇文章 0 订阅
2 篇文章 0 订阅

Given a two-dimensional matrix, your program is to print the matrix elements in a "snail-like" fashion as follows:

  • print elements in the first row from left to right on an output line   
  • print elements in the last column from top to bottom on an output line   
  • print elements in the last row from right to left on an output line  
  • print elements in the first column from bottom to top on an output line   
  • print elements in the second row from left to right on an output line
  • ……

       (Note that, as illustrated in Sample Input/Output, each matrix element is printed only once.)

The Input:

       The input is divided into sets. Each input set starts with two integer values for M and N, dimensions of a matrix (both values between 1 and 15, inclusive, and the two values are separated by at least one space). The next M input lines contain values for matrix rows, one row per input line. Assume that each matrix element is a single character, that input values for matrix elements start in column 1, and that there is exactly one space between matrix elements in the input.

End of data is indicated by values 0 and 0 for M and N.

The Output: 

      For each matrix, print the original version with one space between the elements. Then, print the matrix elements in a"snail-like" fashion with no space between the elements on an output line. Leave a blank line after the output for each matrix. Follow the format illustrated in Sample Output.

样例输入 复制

3 4
A B C D
J K L E
I H G F
5 6
A B C D E F
R S T U V G
Q Z Z Z W H
P Z Z Y X I
O N M L K J
0 0

样例输出 复制

Matrix #1:
Original:
A B C D
J K L E
I H G F
Snail:
ABCD
EF
GHI
J
KL

Matrix #2:
Original:
A B C D E F
R S T U V G
Q Z Z Z W H
P Z Z Y X I
O N M L K J
Snail:
ABCDEF
GHIJ
KLMNO
PQR
STUV
WX
YZZ
Z
ZZ

顺时针螺旋输出矩阵:

Original:

A B C D

J K L E

I H G F

输出一行:

 ABCDEFGHIJKL

void solve()
{
    int row=m-1;
    int col=n-1;
    int index=0;
    int i=0,j=0;
    int up=0,down=row,left=0,right=col;
    while(up<=down&&left<=right)
    {
        if(index==0)
        {
            i=up;
            for(j=left;j<=right;j++)
            {
                cout<<mat[i][j];
            }
            //cout<<"my god"<<endl;
            //cout<<endl;
            up++;
           // index=1;
        }
        else if(index==1)
        {
            j=right;
            for(i=up;i<=down;i++)
            {
                cout<<mat[i][j];
            }
            //cout<<endl;
            right--;
           // index=2;
        }
        else if(index==2)
        {
            i=down;
            for(j=right;j>=left;j--)
            {
                cout<<mat[i][j];
            }
           // cout<<endl;
            down--;
            //index=3;
        }
        else if(index==3)
        {
            j=left;
            for(i=down;i>=up;i--)
            {
                cout<<mat[i][j];
            }
           // cout<<endl;
            left++;
           // index=0;
        }
        if(index==0||index==1||index==2)
            index++;
        else
            index=0;
    }
    return ;
}

分行输出:

Snail:

ABCD

EF

GHI

J

KL

void solve()
{
    int row=m-1;
    int col=n-1;
    int index=0;
    int i=0,j=0;
    int up=0,down=row,left=0,right=col;
    while(up<=down&&left<=right)
    {
        if(index==0)
        {
            i=up;
            for(j=left;j<=right;j++)
            {
                cout<<mat[i][j];
            }
            //cout<<"my god"<<endl;
            cout<<endl;
            up++;
           // index=1;
        }
        else if(index==1)
        {
            j=right;
            for(i=up;i<=down;i++)
            {
                cout<<mat[i][j];
            }
            cout<<endl;
            right--;
           // index=2;
        }
        else if(index==2)
        {
            i=down;
            for(j=right;j>=left;j--)
            {
                cout<<mat[i][j];
            }
            cout<<endl;
            down--;
            //index=3;
        }
        else if(index==3)
        {
            j=left;
            for(i=down;i>=up;i--)
            {
                cout<<mat[i][j];
            }
            cout<<endl;
            left++;
           // index=0;
        }
        if(index==0||index==1||index==2)
            index++;
        else
            index=0;
    }
    return ;
}

ac代码:

#include<iostream>
#include<string.h>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<set>
#include<numeric>
#include<vector>
#include<queue>
#include<array>
#define _USE_MATH_DEFINES
using namespace std;
typedef long long ll;
const int mod=10^9+7;
//tuple<int,int,int>p[100000]
char mat[20][20];
int m,n;
void solve()
{
    int row=m-1;
    int col=n-1;
    int index=0;
    int i=0,j=0;
    int up=0,down=row,left=0,right=col;
    while(up<=down&&left<=right)
    {
        if(index==0)
        {
            i=up;
            for(j=left;j<=right;j++)
            {
                cout<<mat[i][j];
            }
            //cout<<"my god"<<endl;
            cout<<endl;
            up++;
           // index=1;
        }
        else if(index==1)
        {
            j=right;
            for(i=up;i<=down;i++)
            {
                cout<<mat[i][j];
            }
            cout<<endl;
            right--;
           // index=2;
        }
        else if(index==2)
        {
            i=down;
            for(j=right;j>=left;j--)
            {
                cout<<mat[i][j];
            }
            cout<<endl;
            down--;
            //index=3;
        }
        else if(index==3)
        {
            j=left;
            for(i=down;i>=up;i--)
            {
                cout<<mat[i][j];
            }
            cout<<endl;
            left++;
           // index=0;
        }
        if(index==0||index==1||index==2)
            index++;
        else
            index=0;
    }
    return ;
}
int main()
{
    scanf("%d %d",&m,&n);
    int T=1;
    while(m!=0)
    {
        if(T!=1)
            cout<<endl;
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>mat[i][j];
            }
        }
        printf("Matrix #%d:\n",T);
        printf("Original:\n");
        for(int i=0;i<m;i++)
        {
            for(int j=0;j<n;j++)
            {
                cout<<mat[i][j];
                if(j!=n-1)
                    cout<<" ";
                else
                    cout<<endl;
            }
        }
        printf("Snail:\n");
        solve();
        T++;
        scanf("%d %d",&m,&n);
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值