java - Small factorials on SPOJ -
i trying submit code 'small factorials' problem on spoj. runs on ide shows run time error (nzec) on spoj. please suggest solutions.
import java.util.scanner; class factoria { public static void main (string[] args) throws java.lang.exception { int t, n; int multi = 1; scanner tc = new scanner(system.in); t = tc.nextint(); for(int i=0;i<t;i++){ scanner nc = new scanner(system.in); n = nc.nextint(); for(int f=1;f<=n;f++){ multi = multi * f; } system.out.println(multi); multi = 1; } } }
your runtime error because try initiate new scanner
on system.in
when have 1 open. seems fail on java 8 although seems run ok on java 7 - both tested on ideone. there no need initiate new scanner
.
if remove line
scanner nc = new scanner(system.in);
and use tc.nextint();
instead of nc.nextint();
should work.
i note you're doing online competition. should note factorial fail (overflow) if input > 12 you're using int
type. don't know whether apply in case.
bugfixed code
import java.util.scanner; class factoria { public static void main (string[] args) throws java.lang.exception { int t, n; int multi = 1; scanner tc = new scanner(system.in); t = tc.nextint(); for(int i=0;i<t;i++){ n = tc.nextint(); for(int f=1;f<=n;f++){ multi = multi * f; } system.out.println(multi); multi = 1; } } }
input
5 4 3 5 2 1
output
24 6 120 2 1
Comments
Post a Comment