I got TLE for this problem but can't think of anything that would make my submission any faster.
Code: Select all
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.PrintStream;
import java.util.Scanner;
class Main
{
void begin()
{
Scanner input = new Scanner ( new BufferedInputStream ( System.in ) );
PrintStream output = new PrintStream ( new BufferedOutputStream ( System.out ) );
int n = 0;
while ( ( n = input.nextInt() ) != 0 )
{
output.format ( "f91(%d) = %d%n", n, n > 100 ? n - 10 : 91 );
}
output.flush();
}
public static void main ( String[] args )
{
Main my_submission = new Main();
my_submission.begin();
}
}
Feeding it my worst case test file (250000 random integers 1 to 1000000 inclusive) it takes about 10s.
I've tried tinkering with the buffer size but success eludes me. Results so far include:
- OutOfMemoryError (various forms)
- no actual output despite flush (this puzzles me)
- sub 3s runtime but incomplete output (doesn't actually produce all lines of output, only the last 100 or so)
I've tried an ANSI C solution but it appears to run even slower than my Java?!
Code: Select all
#include <stdio.h>
int main ()
{
unsigned n = 0;
while ( scanf ( "%u\n", &n ), n != 0 )
{
printf ( "f91(%u) = %u\n", n, n > 100 ? n - 10 : 91 );
}
return 0;
}
Regards,
Alex C.