Sword refers to offer: print the matrix clockwise

Source: Internet
Author: User
Tags exit include printf

Sword refers to the 20th question on offer, nine degrees OJ on the test pass.

Topic Description:

Enter a matrix and print each number in clockwise order from the outside, for example, if you enter the following matrix:

1 2 3 4

5 6 7 8

9 10 11 12

13 14 15 16

Then print the 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 of the words in sequence.

Input:

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

The first line entered consists of two integers m and n (1<=m,n<=1000): The dimension of the representation matrix is m row n columns.

Back to the column page: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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

Output:

corresponding to each test case, the output line,

Print each digit in 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:

1 2 3 4 8 12 16 15 14 13 9 5 6 7-11 10

This topic does not involve any algorithm, mainly study logical thinking, pay attention to the analysis of various situations, the conditions of circular exit. This topic is not the same as before, the output should pay special attention to the output of the last output element format.

The

AC code is as follows:

#include <stdio.h> #include <stdlib.h>/* Clockwise print one lap/void printmatrixcirclely (int **num,int m,int  
    N,int start) {int endx = n-start-1;  
      
    int EndY = m-start-1;  
    int i;  
      
    Print rows from left to right for (i=start;i<=endx;i++) printf ("%d", num[start][i]); Print columns from top to bottom if (Start < EndY) {for (i=start+1;i<=endy;i++) printf ("%d", num[i][endx  
    ]);  
            //Right-to-left Print rows if (start<endx && Start<endy) {for (i=endx-1;i>=start;i--)  
    printf ("%d", num[endy][i]); //From bottom to top print column if (start<endx && start<endy-1) {for (i=endy-1;i>=start+1;i  
        --) {printf ("%d", Num[i][start]); * * Clockwise print matrix/void printmatrixclockwisely (int **num,int m,int n) {if num==null | | m<= 0 | |  
    n<=0) return;  
    int start = 0; while (M&GT;2*start && N>2*start) {printmatrixcirclely (Num,m,n,start);  
    start++;  
    int main () {int m,n;  
        while (scanf ("%d%d", &m,&n)!= EOF) {int **num = (int * *) malloc (m*sizeof (int *));  
        if (num = NULL) exit (exit_failure);  
        int i;  
            for (i=0;i<m;i++) {num[i] = (int *) malloc (n*sizeof (int));  
        if (num[i] = = NULL) exit (exit_failure);  
        Int J;  
      
        for (i=0;i<m;i++) for (j=0;j<n;j++) scanf ("%d", &num[i][j]);  
        Printmatrixclockwisely (Num,m,n);  
      
        printf ("\ n");  
            for (i=0;i<m;i++) {free (num[i]);  
        Num[i] = NULL;  
        Free (num);       
    num = NULL;  
return 0; }

Spit Trough: Weekend General to University City study, Monday to Friday evening General go to the library to study, these points of learning efficiency is the highest! In view of laboratory pit fall attendance rules, Monday to Friday in the afternoon had to stay in the laboratory study, had to say that the efficiency of this time period is really sometimes good, sometimes bad, simply open the question before the time to write a report on the problem!

Now the lab is really a little emotional, full of negative energy. Disappointed here, a place that has been erased, and a disappointment to the people here, it's really hard to find a kindred soul.

Spit trough to spit trough, in fact, has long been accustomed to, after all, in the circle in the inside for a year. After graduation, everyone will make plans for their future, who can control who? Like-Minded people can achieve each other, and like-minded people will only destroy each other. Perhaps everyone's values are different, but the reality is to face sooner or later, not understanding the hard work of parents, will never grow up.

I am very lucky, in the csdn met a lot of a common pursuit of small partners, but also very fortunate to find a lot of positive energy here. Some people have got good offer, some are still trying, never give up, these are the people who deserve respect and study.

Anyone, as long as there is a struggle for the heart, and for a long time to insist and pay, life will not be mistreated him.

Author: csdn Blog Lan pavilion Wind and Rain

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.