题目:
遇见的问题:
这道题目非常简单,但是看到时间限制5s,很是诧异。抱着试试得态度把java得代码提交,发现最高得90分,而且是由于内存超出。代码中只有两个对象:一个是二维数组长度最大为100W,另一个是输入得Scanner;由于100W的数组内存都不会超出10MB,所以只能把问题定焦在输入上。在网上看到大佬的分析,https://blog.youkuaiyun.com/weixin_35040169/article/details/79700540,茅塞顿开,nextInt的IO操作非常耗时,本题最高需要输入100W次,nextInt会非常非常慢,需要改用BufferReader进行输入。
代码:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
Scanner sc=new Scanner(System.in);
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str;String[] temp;
str = br.readLine();
temp = str.split(" ");
int n = Integer.parseInt(temp[0]);
int m = Integer.parseInt(temp[1]);
int a[][]=new int[1001][1001];
for(int i=0;i<n;i++) {
str = br.readLine();
temp = str.split(" ");
for(int j=0;j<temp.length;j++) {
a[i][j]= Integer.parseInt(temp[j]);
}
}
for(int i=m-1;i>=0;i--) {
for(int j=0;j<n;j++) {
System.out.print(a[j][i]);
if(j!=n-1) System.out.print(" ");
else System.out.println();
}
}
}
}