201503-1 图像旋转 Java

import java.util.Scanner;
//得分80,本题最高需要输入100W次,因为nextInt很耗时,导致内存超了
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int m = sc.nextInt();
        int a[][] = new int[n][m];
        for(int i=0;i<n;i++) {
            for(int j=0;j<m;j++) {
                a[i][j] = sc.nextInt();
            }
        }
        int count = 0;
        for(int j=m-1;j>=0;j--) {
            for(int i=0;i<n;i++) {
                System.out.print(a[i][j] + " ");
                count++;
                if(count % n == 0) {
                    System.out.println();
                }
            }
        }
        sc.close();
    }

}

下面还有

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
//100分,用BufferedReader
public class Main {
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        BufferedReader buff = new BufferedReader(new InputStreamReader(System.in));
        String str;
        String [] temp;
        
        str = buff.readLine();
        temp = str.split(" ");
        int n = Integer.parseInt(temp[0]);
        int m = Integer.parseInt(temp[1]);
        int a[][] = new int[n][m];
        for(int i=0;i<n;i++) {
            str = buff.readLine();
            temp = str.split(" ");
            for(int j=0;j<m;j++) {
                a[i][j] = Integer.parseInt(temp[j]);
            }
        }
        for(int j=m-1;j>=0;j--) {
            for(int i=0;i<n;i++) {
                System.out.print(a[i][j] + " ");
                if(i == n-1) {
                    System.out.println();
                }
            }
        }
        sc.close();
    }

}

猜你喜欢

转载自www.cnblogs.com/yu-jiawei/p/12343204.html