I just did USACO/Number Triangles ex and found a problem about read file.I used "Scanner" to read in the file first, but test 9 was failed, because the running time was 1.8secs, which exceeded the limitation 1.3secs.Then, I used "StreamTokenizer" to read in the file, finally all tests were cleared. At this time, test 9 only took 0.38secs. I do not know why there was such big difference between "Scanner" and "StreamTokenizer".
"Scanner" code:
Scanner sc = new Scanner(new FileReader("numtri.in"));
int n= sc.nextInt();
int Array[][] = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
Array[i][j]=sc.nextInt();
}
}
"StreamTokenizer" code:
StreamTokenizer sc = new StreamTokenizer(new BufferedReader(new FileReader("numtri.in")));
int nextInt() throws IOException
{
sc.nextToken();
return (int)sc.nval;
}
int n = nextInt();
int Array[][] = new int[n][n];
for(int i=0;i<n;i++){
for(int j=0;j<=i;j++){
Array[i][j]=nextInt();
}
}