Special matrix printing and rotation

Special matrix printing and rotation

A matrix print clockwise

The meaning of problems:
Given n, m represents a matrix of rows and columns, the value of the entire matrix has, then print the matrix in a clockwise direction.

Example:
Here Insert Picture Description

Solution:
You can print each lap as a cycle;
namely:
The first step: print from left to right;
Step two: Print from top to bottom;
the third step: from right to left printing;
Step four: from next to the printing;

Wherein for each cycle, the first step will certainly be carried out, but other steps may not proceed, so special to be sentenced, see specific codes:

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define lowbit(x) x&(-x)
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const double pi=3.14159;
const int maxn=1e5;
const int mod=1e3;
int mp[210][210];
int n,m;
vector<int>ve;         //存结果
void printOneCircle(int start){
    int endY=m-start-1;
    int endX=n-start-1;
    //从左到右
    for(int j=start;j<=endY;j++){
        ve.push_back(mp[start][j]);
    }
    //从上到下
    if(start<endX){
        for(int i=start+1;i<=endX;i++){
            ve.push_back(mp[i][endX]);
        }
    }
    //从右到左
    if(start<endX&&start<endY){
        for(int j=endY-1;j>=start;j--){
            ve.push_back(mp[endX][j]);
        }
    }
    //从下到上
    if(start<endX&&start<endY-1){
        for(int i=endX-1;i>=start+1;i--){
            ve.push_back(mp[i][start]);
        }
    }
}
int main(){
    scanf("%d%d",&n,&m);
    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
            scanf("%d",&mp[i][j]);
        }
    }
    int start=0;
    while(n>=2*start&&m>=2*start){
        printOneCircle(start);
        start++;
    }
    printf("%d",ve[0]);
    for(int i=1;i<ve.size();i++){
        cout<<" "<<ve[i];
    }
    cout<<endl;
    return 0;
}

Second, according to the matrix font Print

The meaning of problems:
Given n, m represents a matrix of rows and columns, the value of the entire matrix has, and then follow the zigzag print matrix.

answer:

Third, the matrix 90 degrees clockwise

The meaning of problems:
Given n denotes a matrix of size n * n, then gives the values of the respective positions of matrix
matrix matrix after the request is rotated 90 degrees clockwise

Solution:
After find the law under find:
B [I] [J] = a [n--. 1-J] [I]
(wherein a represents the original matrix, b represents the matrix after rotation obtained)
I, J starts from 0

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<string>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#define lowbit(x) x&(-x)
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const double pi=3.14159;
const int maxn=1e5;
const int mod=1e3;
int a[210][210];
int n;
int b[210][210];
int main(){
    scanf("%d",&n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            scanf("%d",&a[i][j]);
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            b[i][j]=a[n-j-1][i];
        }
    }
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            if(j==n-1)
                printf("%d\n",b[i][j]);
            else{
                printf("%d ",b[i][j]);
            }
        }
    }
    return 0;
}
Published 127 original articles · won praise 32 · views 10000 +

Guess you like

Origin blog.csdn.net/boliu147258/article/details/104348421