给定一个n*n的矩阵matrix,请把这个矩阵顺时针转动90度。
输入包含多行,第一行一个整数n,代表矩阵的行数和列数,接下来n行,每行n个整数,代表矩阵matrix
。
输出旋转后的矩阵(包含n行,每行n个数)。
4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
13 9 5 1 14 10 6 2 15 11 7 3 16 12 8 4
额外空间复杂度。
#include<iostream>
#include<vector>
using namespace std;
void rotateEdge(vector<vector<int>>& arr, int leftTopX, int leftTopY, int rightBottomX, int rightBottomY) {
int groupNums = rightBottomX - leftTopX; // 组数
for(int i = 0; i < groupNums; i++){
int temp = arr[leftTopX][leftTopY + i];
arr[leftTopX][leftTopY + i] = arr[rightBottomX - i][leftTopY];
arr[rightBottomX - i][leftTopY] = arr[rightBottomX][rightBottomY - i];
arr[rightBottomX][rightBottomY - i] = arr[leftTopX + i][rightBottomY];
arr[leftTopX + i][rightBottomY] = temp;
}
}
int main(){
int n;
cin >> n;
vector<vector<int>> arr(n, vector<int>(n, 0));
for(int i = 0; i < n; i++)
for(int j = 0; j < n; j++) cin >> arr[i][j];
int leftTopX = 0, leftTopY = 0;
int rightBottomX = n - 1, rightBottomY = n - 1;
while(leftTopX <= rightBottomX && leftTopY <= rightBottomY)
rotateEdge(arr, leftTopX++, leftTopY++, rightBottomX--, rightBottomY--);
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++) cout << arr[i][j] << " ";
cout << "\n";
}
return 0;
} import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
String[][] matrix = new String[n][n];
for(int i = 0; i < n; i++)
matrix[i] = br.readLine().trim().split(" ");
for(int i = 0; i < n; i++){
for(int j = 0; j < i; j++){
String temp = matrix[i][j];
matrix[i][j] = matrix[j][i];
matrix[j][i] = temp;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n / 2; j++){
String temp = matrix[i][j];
matrix[i][j] = matrix[i][n - j - 1];
matrix[i][n - j - 1] = temp;
}
}
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++)
System.out.print(matrix[i][j] + " ");
System.out.println();
}
}
} #include <bits/stdc++.h>
using namespace std;
int main(){
int n;
cin>>n;
int a[n][n];
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
cin>>a[i][j];
for(int j=0;j<n;j++)
for(int i=n-1;i>=0;i--){
if(i==0)
printf("%d\n", a[i][j]);
else
printf("%d ", a[i][j]);
}
return 0;
} import java.util.Scanner;
public class Main {
public static void rotate(int[][] matrix) {
int tR = 0;
int tC = 0;
int dR = matrix.length - 1;
int dC = matrix[0].length - 1;
while (tR < dR) {
rotateEdge(matrix, tR++, tC++, dR--, dC--);
}
}
public static void rotateEdge(int[][] matrix, int tR, int tC, int dR, int dC) {
int times = dR - tR;
for (int i = 0; i < times; i++) {
int temp = matrix[tR][tC + i];
matrix[tR][tC + i] = matrix[dR - i][tC];
matrix[dR - i][tC] = matrix[dR][dC - i];
matrix[dR][dC - i] = matrix[tR + i][dC];
matrix[tR + i][dC] = temp;
}
}
public static void printMatrix(int[][] matrix) {
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix[0].length; j++) {
System.out.print(matrix[i][j] + " ");
}
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] matrix = new int[n][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
matrix[i][j] =sc.nextInt();
}
}
rotate(matrix);
printMatrix(matrix);
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String firstLine = sc.nextLine();
int n = Integer.valueOf(firstLine);
int[][] matrix = new int[n][n];
for(int i=0; i<n; i++){
String line = sc.nextLine();
String[] array = line.split(" ");
for(int j=0; j<n; j++){
matrix[i][j] = Integer.valueOf(array[j]);
}
}
rotateMatrix(matrix);
printSquareMatrix(matrix);
}
public static void rotateEage(int[][] m, int tR, int tC, int dR, int dC){
int temp;
for(int i=0; i<dC-tC; i++){
temp = m[tR][tC+i];
m[tR][tC+i] = m[dR-i][tC];
m[dR-i][tC] = m[dR][dC-i];
m[dR][dC-i] = m[tR+i][dC];
m[tR+i][dC] = temp;
}
}
public static void rotateMatrix(int[][] m){
int tR = 0;
int tC = 0;
int dR = m.length - 1;
int dC = m.length - 1;
while(tC < dC){
rotateEage(m, tR++, tC++, dR--, dC--);
}
}
public static void printSquareMatrix(int[][] m){
int n = m.length;
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.print(m[i][j] + " ");
}
System.out.println();
}
}
} n=int(input()) matrix=[[0]*n for i in range(n)] for i in range(n): matrix[i]=list(map(int,input().split())) #先将矩阵倒序,然后行列互换 matrix[::] = [[row[i] for row in matrix[::-1]] for i in range(len(matrix[0]))] for i in range(n): for j in range(n): print(matrix[i][j],end=' ') print()
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine().trim());
String[][] arr = new String[n][n];
for(int i=0;i<n;i++){
arr[i] = br.readLine().trim().split(" ");
}
rotate(arr);
StringBuilder sb = new StringBuilder();
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
sb.append(arr[i][j]).append(" ");
}
sb.append("\n");
}
System.out.print(sb.toString());
}
private static void rotate(String[][] arr){
if(arr==null||arr.length<1) return;
int tr=0;
int tc=0;
int dr=arr.length-1;
int dc = arr[0].length-1;
while(tr<=dr&&tc<=dc){
rotateEdge(arr,tr++,tc++,dr--,dc--);
}
}
private static void rotateEdge(String[][] arr,int tr,int tc,int dr,int dc){
int t = dc-tc;
String temp;
for(int i=0;i<t;i++){
temp = arr[tr][tc+i];
arr[tr][tc+i] = arr[dr-i][tc];
arr[dr-i][tc] = arr[dr][dc-i];
arr[dr][dc-i] = arr[tr+i][dc];
arr[tr+i][dc] = temp;
}
}
}
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = Integer.parseInt(sc.nextLine());
int[][] two = new int[n][n];
for(int i = 0; i < n; i++){
String s = sc.nextLine();
String[] ss = s.split(" ");
for(int j = 0; j < n; j++){
two[i][j] = Integer.parseInt(ss[j]);
}
}
int[][] one = new int[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
System.out.print(two[n-j-1][i]+" ");
}
System.out.println();
}
}
} #include <iostream>
#include <vector>
using namespace std;
class Solution
{
public:
void rotate(vector<vector<int>>& matrix)
{
int sr = 0;
int sc = 0;
int er = matrix.size() - 1;
int ec = matrix[0].size() - 1;
while (sr < er)
{
rotateEdge(matrix, sr, sc, er, ec);
sr++;
sc++;
er--;
ec--;
}
}
private:
/*
旋转一条边
(a,b) 表示左上角的点
(c,d) 表示右下角的点
*/
void rotateEdge(vector<vector<int>>& matrix, int a, int b, int c, int d)
{
int temp = 0;
for (int i = 0; i < c - a; i++)
{
temp = matrix[a][b + i];
matrix[a][b + i] = matrix[c - i][b];
matrix[c - i][b] = matrix[c][d - i];
matrix[c][d - i] = matrix[a + i][d];
matrix[a + i][d] = temp;
}
}
};
int main()
{
int n = 0;
cin >> n;
vector<vector<int>> matrix(n, vector<int>(n));
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cin >> matrix[i][j];
}
}
Solution s;
s.rotate(matrix);
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
cout << matrix[i][j] << " ";
}
cout << endl;
}
return 0;
}