Clockwise print matrix

Source: Internet
Author: User

Description: enter a matrix and print each number in clockwise order from the external direction. For example, if you enter the following matrix: 1 2 3 45 6 7 89 10 11 1213 14 15 16 the numbers 1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10 are printed in sequence. input: the input may contain multiple test examples. For each test case, the first line of the input contains two integers m and n (1 <= m, n <= 1000 ): indicates that the dimension of the matrix is m rows and n columns. In the next m row, each row contains n integers, indicating the elements of the matrix. The value range of each element a is (1 <= a <= 10000 ). Output: Output a line for each test case. Each number is printed clockwise from the external direction. Each number is followed by a space. Sample input: 4 41 2 3 45 6 7 89 10 11 1213 14 15 16 sample output: 1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10 Code AC: Thought: There is no Algorithm for this question. Just control the boundary ~ [Cpp] # include <stdio. h> typedef struct mat {int num; int flag;} mat; int main () {mat ma [1000] [1000]; int I, j, m, n, dir, count; while (scanf ("% d", & m, & n )! = EOF) {for (I = 0; I <m; I ++) {for (j = 0; j <n; j ++) {scanf ("% d", & ma [I] [j]. num); ma [I] [j]. flag = 0 ;}} dir = 0; // left-bottom-right-top loop count = 0; I = 0; j = 0; while (count <n * m) {switch (dir) {case 0: if (j <n) {if (! Ma [I] [j]. flag) {printf ("% d", ma [I] [j]. num); ma [I] [j]. flag = 1; count ++; j ++;} else {j --; I ++; dir + = 1; dir % = 4 ;}} else {j --; I ++; dir + = 1; dir % = 4;} break; case 1: if (I <m) {if (! Ma [I] [j]. flag) {printf ("% d", ma [I] [j]. num); ma [I] [j]. flag = 1; count ++; I ++;} else {I --; j --; dir + = 1; dir % = 4 ;}} else {I --; j --; dir + = 1; dir % = 4;} break; case 2: if (j> = 0) {if (! Ma [I] [j]. flag) {printf ("% d", ma [I] [j]. num); ma [I] [j]. flag = 1; count ++; j --;} else {j ++; I --; dir + = 1; dir % = 4 ;}} else {j ++; I --; dir + = 1; dir % = 4;} break; case 3: if (I> = 0) {if (! Ma [I] [j]. flag) {www.2cto.com printf ("% d", ma [I] [j]. num); ma [I] [j]. flag = 1; count ++; I --;} else {I ++; j ++; dir + = 1; dir % = 4 ;}} else {I ++; j ++; dir + = 1; dir % = 4;} break ;}} printf ("\ n") ;}return 0 ;}

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.