首页 > 其他 > 详细

矩阵上下左右翻转

时间:2016-04-26 22:08:08      阅读:138      评论:0      收藏:0      [点我收藏+]


一:上下翻转

    上下翻转的遍历顺序是从最下面一行开始从左到右遍历,如图:

                技术分享             

#include<iostream>
#include<stdio.h>
using namespace std;
/**
  上下翻转,
*/
int main(){
  int M=4,N=3;
  int temp=0;
  int matrix[4][3] = {1,2,3,
                      4,5,6,
                      7,8,9,
                      10,11,12};
   printf("上下翻转之前\n");
for(int p1=0;p1<M;p1++)
    {
        for(int j=0;j<N;j++)
            printf("%d ",matrix[p1][j]);
        printf("\n");
    }


 for(int p=0;p<M/2;p++){
            for(int q=0;q<N;q++){
                temp=matrix[p][q];
                matrix[p][q]=matrix[M-p-1][q];
                matrix[M-p-1][q]=temp;
            }
        }
    printf("\n");
    printf("============华丽的分割线=============\n\n");
    printf("上下翻转之后\n");
for(int p1=0;p1<M;p1++)
    {
        for(int j=0;j<N;j++)
            printf("%d ",matrix[p1][j]);
        printf("\n");
    }

  return 0;
}


=============================我是一根傲娇的分割线===========================


二:左右翻转

   原理差不多

#include<iostream>
#include<stdio.h>
using namespace std;
/**
  左右翻转,
*/
int main(){
  int M=4,N=3;
  int temp=0;
  int matrix[4][3] = {1,2,3,
                      4,5,6,
                      7,8,9,
                      10,11,12};
   printf("左右翻转之前\n");
for(int p1=0;p1<M;p1++)
    {
        for(int j=0;j<N;j++)
            printf("%d ",matrix[p1][j]);
        printf("\n");
    }


 for(int p=0;p<M;p++)
        {
            for(int q=0;q<N/2;q++)
            {
                temp=matrix[p][q];
                matrix[p][q]=matrix[p][N-q-1];
                matrix[p][N-q-1]=temp;
            }
        }
    printf("\n");
    printf("============华丽的分割线=============\n\n");
    printf("左右翻转之后\n");
for(int p1=0;p1<M;p1++)
    {
        for(int j=0;j<N;j++)
            printf("%d ",matrix[p1][j]);
        printf("\n");
    }

  return 0;
}




矩阵上下左右翻转

原文:http://blog.csdn.net/qq_26891045/article/details/51228023

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!