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();
	}

}

下面改用BufferedReader

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();
	}

}

posted @ 2020-02-21 21:29  菜鸡A  阅读(127)  评论(0编辑  收藏  举报