Hackerrank – Problem Statement
A description of the problem can be found on Hackerrank.
Solution
I created solution in:
All solutions are also available on my GitHub.
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
import java.util.*; import java.security.*; public class Solution { public static void main(String[] args) { DoNotTerminate.forbidExit(); try { Scanner in = new Scanner(System.in); int n = in.nextInt(); in.close(); String s = String.valueOf(n); if (n == Integer.parseInt(s)) { System.out.println("Good job"); } else { System.out.println("Wrong answer."); } } catch (DoNotTerminate.ExitTrappedException e) { System.out.println("Unsuccessful Termination!!"); } } } //The following class will prevent you from terminating the code using exit(0)! class DoNotTerminate { public static class ExitTrappedException extends SecurityException { private static final long serialVersionUID = 1; } public static void forbidExit() { final SecurityManager securityManager = new SecurityManager() { @Override public void checkPermission(Permission permission) { if (permission.getName().contains("exitVM")) { throw new ExitTrappedException(); } } }; System.setSecurityManager(securityManager); } } |