zoukankan      html  css  js  c++  java
  • 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();
    	}
    
    }
    
    
  • 相关阅读:
    vue+vant打包,vue+vant-ui小程序,微信支付
    vue+vant-ui移动端适配 宽高
    web前端面试题
    vue面试题及答案(1)
    vue的增删改查(简单版)
    Vue computed计算属性
    vue.cli的安装配置
    Create React App 安装时出现的错误解决方法
    运行node文件的多种方式
    怎么把node配置成全局打开
  • 原文地址:https://www.cnblogs.com/yu-jiawei/p/12343204.html
Copyright © 2011-2022 走看看