The clockwise printing matrix of the sword point

Source: Internet
Author: User

Title Description:

Enter a matrix that prints each number in a clockwise order from outward, for example, if you enter the following matrix:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

The word 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 is printed in turn.

Input:

The input may contain multiple test samples, for each test case,

The first line of input includes two integers m and n (1<=m,n<=1000): Represents the number of dimensions of the matrix as M row n columns.

The next M-line, each row consists of n integers, representing the elements of the matrix, where each element A has a value range of (1<=a<=10000).

Output:

For each test case, output one line,

Print each number in a clockwise order from the outside, with a space behind each number.

Sample input:
4 41 2 3 45 6 7 89 10 11 1213 14 15 16
Sample output:


Code:
#include <cstdio>using namespacestd;intarr[1010][1010]; voidPrintmatrixincircle (intRowsintColumnsintstart) {    intendx=rows-1-start; intendy=columns-1-start;  for(inti=start;i<=endy;++i) printf ("%d", Arr[start][i]); if(start<EndX) {         for(inti=start+1; i<=endx;++i) printf ("%d", Arr[i][endy]); }    if(start<endx&&start<EndY) {         for(inti=endy-1; i>=start;--i) printf ("%d", Arr[endx][i]); }    if(start<endx-1&&start<EndY) {         for(inti=endx-1; i>start;--i) printf ("%d", Arr[i][start]); }} intMain () {intn,m;  while(SCANF ("%d%d", &n,&m)! =EOF) {         for(intI=0; i<n;++i) for(intj=0; j<m;++j) scanf ("%d",&Arr[i][j]); intstart=0; intRows=N; intcolumns=m;  while(rows>start*2&&columns>start*2) {printmatrixincircle (Rows,columns,start); ++start; } printf ("\ n"); }    return 0;} /************************************************************** problem:1391 User:lcyvino language:c++ Re sult:accepted time:520 Ms memory:5504 kb****************************************************************/

The clockwise printing matrix of the sword point

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.