zoukankan      html  css  js  c++  java
  • 数据结构之顺时针打印矩阵

    顺时针打印矩阵

    分析:把矩阵想象成若干个圈,用一个循环打印矩阵,每次打印矩阵的一个圈


    #include<stdio.h>
    
    #define MAX 1001
    
    int matrix[MAX][MAX]={{1,2,3,4},{5, 6, 7, 8},{9, 10, 11, 12},{13, 14, 15, 16}};
    
    void clockwise(int m,int n,int start){
        int xend=m-1-start;
        int yend=n-1-start;
    
        int i,j;
        //left -right
        for(j=start;j<=yend;j++){
            printf("%d ",matrix[start][j]);
        }
    
        //top down
        for(i=start+1;i<=xend;i++){
            printf("%d ",matrix[i][yend]);
        }
    
        //right left
        for(j=yend-1;j>=start;j--){
            printf("%d ",matrix[xend][j]);
        }
    
        //down top
        for(i=xend-1;i>=start+1;i--){
            printf("%d ",matrix[i][start]);
        }
    
    }
    
    void fun(int m,int n){
        int start=0;//一圈一圈的打印
        while(m>start*2&&n>start*2){//结束条件
            clockwise(m,n,start);
            start++;
        }
    }
    
    
    int main()
    {
        int m=4,n=4;
        fun(m,n);
        return 0;
    }
    


  • 相关阅读:
    Codeforces 798C
    Codeforces 798B
    Codeforces 798A
    HDU
    HDU
    HDU
    加速cin的技巧
    Codeforces Gym
    Codeforces Gym
    Solutions to an Equation LightOJ
  • 原文地址:https://www.cnblogs.com/jasonhaven/p/7355030.html
Copyright © 2011-2022 走看看