蓝桥杯JAVA-32.二维数组(矩阵)实现旋转模板(JAVA实现)

个人博客
www.tothefor.com
蓝桥杯复习知识点汇总

目录

顺时针旋转

90°

import java.io.*;
import java.math.BigInteger;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @墨水记忆 www.tothefor.com
 */
public class Main {
    
    
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);
    public static int monthes[]= {
    
    0,31,28,31,30,31,30,31,31,30,31,30,31};//一年中的12个月每月的天数
    public static int maxd = 1000+7;
    public static int INF = 0x3f3f3f3f;
    public static int mod = 998244353;

    public static int[][] a = new int[maxd][maxd];
    public static int[][] b = new int[maxd][maxd];


    public static void main(String[] args) throws Exception {
    
    

        int n = nextInt(); //行
        int m = nextInt(); //列
        for(int i=0;i<n;++i){
    
    
            for(int j=0;j<m;++j){
    
    
                a[i][j]=nextInt();
            }
        }
        //旋转后为m行n列
        for(int i=0;i<m;++i){
    
     //行
            for(int j=0;j<n;++j){
    
     //列
                b[i][j]=a[n-1-j][i];
            }
        }
        for(int i=0;i<m;++i){
    
    
            for(int j=0;j<n;++j){
    
    
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }


        closeAll();
    }


    public static int gcd(int a,int b){
    
     // 不需要判断a和b的大小
        while(b>0){
    
    
            a%=b;b^=a;a^=b;b^=a;
//           while(b^=a^=b^=a%=b);
        }
        return a;
//        return (a % b == 0) ? b : gcd(b, a%b);
    }

    public static void cinInit(){
    
    
        cin.wordChars('a', 'z');
        cin.wordChars('A', 'Z');
        cin.wordChars(128 + 32, 255);
        cin.whitespaceChars(0, ' ');
        cin.commentChar('/');
        cin.quoteChar('"');
        cin.quoteChar('\'');
        cin.parseNumbers(); //可单独使用还原数字
    }
    public static int nextInt() throws Exception {
    
    
        cin.nextToken();
        return (int) cin.nval;
    }
    public static long nextLong() throws Exception {
    
    
        cin.nextToken();
        return (long) cin.nval;
    }
    public static double nextDouble() throws Exception {
    
    
        cin.nextToken();
        return cin.nval;
    }
    public static String nextString() throws Exception {
    
    
        cin.nextToken();
        return cin.sval;
    }
    public static void closeAll() throws Exception {
    
    
        cout.close();
        in.close();
        out.close();
    }
}

180°

import java.io.*;
import java.math.BigInteger;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @墨水记忆 www.tothefor.com
 */
public class Main {
    
    
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);
    public static int monthes[]= {
    
    0,31,28,31,30,31,30,31,31,30,31,30,31};//一年中的12个月每月的天数
    public static int maxd = 1000+7;
    public static int INF = 0x3f3f3f3f;
    public static int mod = 998244353;

    public static int[][] a = new int[maxd][maxd];
    public static int[][] b = new int[maxd][maxd];


    public static void main(String[] args) throws Exception {
    
    

        int n = nextInt(); //行
        int m = nextInt(); //列
        for(int i=0;i<n;++i){
    
    
            for(int j=0;j<m;++j){
    
    
                a[i][j]=nextInt();
            }
        }
        //旋转后还是为 n行 m列
        for(int i=0;i<n;++i){
    
     //行
            for(int j=0;j<m;++j){
    
     //列
                 b[i][j]=a[n-1-i][m-1-j];
            }
        }
        for(int i=0;i<n;++i){
    
    
            for(int j=0;j<m;++j){
    
    
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }


        closeAll();
    }


    public static int gcd(int a,int b){
    
     // 不需要判断a和b的大小
        while(b>0){
    
    
            a%=b;b^=a;a^=b;b^=a;
//           while(b^=a^=b^=a%=b);
        }
        return a;
//        return (a % b == 0) ? b : gcd(b, a%b);
    }

    public static void cinInit(){
    
    
        cin.wordChars('a', 'z');
        cin.wordChars('A', 'Z');
        cin.wordChars(128 + 32, 255);
        cin.whitespaceChars(0, ' ');
        cin.commentChar('/');
        cin.quoteChar('"');
        cin.quoteChar('\'');
        cin.parseNumbers(); //可单独使用还原数字
    }
    public static int nextInt() throws Exception {
    
    
        cin.nextToken();
        return (int) cin.nval;
    }
    public static long nextLong() throws Exception {
    
    
        cin.nextToken();
        return (long) cin.nval;
    }
    public static double nextDouble() throws Exception {
    
    
        cin.nextToken();
        return cin.nval;
    }
    public static String nextString() throws Exception {
    
    
        cin.nextToken();
        return cin.sval;
    }
    public static void closeAll() throws Exception {
    
    
        cout.close();
        in.close();
        out.close();
    }
}

270°

import java.io.*;
import java.math.BigInteger;
import java.util.*;


/**
 * @Author DragonOne
 * @Date 2021/12/5 21:27
 * @墨水记忆 www.tothefor.com
 */
public class Main {
    
    
    public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    public static BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
    public static StreamTokenizer cin = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
    public static PrintWriter cout = new PrintWriter(new OutputStreamWriter(System.out));
    public static Scanner sc = new Scanner(System.in);
    public static int monthes[]= {
    
    0,31,28,31,30,31,30,31,31,30,31,30,31};//一年中的12个月每月的天数
    public static int maxd = 1000+7;
    public static int INF = 0x3f3f3f3f;
    public static int mod = 998244353;

    public static int[][] a = new int[maxd][maxd];
    public static int[][] b = new int[maxd][maxd];


    public static void main(String[] args) throws Exception {
    
    

        int n = nextInt(); //行
        int m = nextInt(); //列
        for(int i=0;i<n;++i){
    
    
            for(int j=0;j<m;++j){
    
    
                a[i][j]=nextInt();
            }
        }
        //旋转后为 m行 n列
        for(int i=0;i<m;++i){
    
     //行
            for(int j=0;j<n;++j){
    
     //列
                b[i][j]=a[j][m-1-i];
            }
        }
        for(int i=0;i<m;++i){
    
    
            for(int j=0;j<n;++j){
    
    
                System.out.print(b[i][j]+" ");
            }
            System.out.println();
        }


        closeAll();
    }


    public static int gcd(int a,int b){
    
     // 不需要判断a和b的大小
        while(b>0){
    
    
            a%=b;b^=a;a^=b;b^=a;
            //           while(b^=a^=b^=a%=b);
        }
        return a;
        //        return (a % b == 0) ? b : gcd(b, a%b);
    }

    public static void cinInit(){
    
    
        cin.wordChars('a', 'z');
        cin.wordChars('A', 'Z');
        cin.wordChars(128 + 32, 255);
        cin.whitespaceChars(0, ' ');
        cin.commentChar('/');
        cin.quoteChar('"');
        cin.quoteChar('\'');
        cin.parseNumbers(); //可单独使用还原数字
    }
    public static int nextInt() throws Exception {
    
    
        cin.nextToken();
        return (int) cin.nval;
    }
    public static long nextLong() throws Exception {
    
    
        cin.nextToken();
        return (long) cin.nval;
    }
    public static double nextDouble() throws Exception {
    
    
        cin.nextToken();
        return cin.nval;
    }
    public static String nextString() throws Exception {
    
    
        cin.nextToken();
        return cin.sval;
    }
    public static void closeAll() throws Exception {
    
    
        cout.close();
        in.close();
        out.close();
    }
}

猜你喜欢

转载自blog.csdn.net/qq_63593632/article/details/123074062