@godzila555 wrote:
i have come across lots of java codes for some problems which seem easy to solve, but some people write codes which include additional classes and the program runs faster. for example there is an easy task: http://www.spoj.com/problems/SMPCIRC/
i saw some kinds of solutions of this problem. one of them is this:import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { static class CircleCoord { int cX; int cY; int r; public CircleCoord(int cx, int cy, int r) { this.cX = cx; this.cY = cy; this.r = r; } public int getXTerm(int x) { return (x - cX) * (x - cX); } public int getYTerm(int y) { return (y - cY) * (y - cY); } } static class TestCase { CircleCoord o1; CircleCoord o2; public TestCase(CircleCoord o1, CircleCoord o2) { this.o1 = o1; this.o2 = o2; } public CircleCoord getO1() { return o1; } public CircleCoord getO2() { return o2; } } public static void main(String[] args) { TestCase[] testCases = readTestCases(); if (testCases == null) { testCases = new TestCase[0]; } for (TestCase testCase : testCases) { CircleCoord o1 = testCase.getO1(); CircleCoord o2 = testCase.getO2(); double distance = Math.sqrt(Math.pow((o1.cX - o2.cX), 2) + Math.pow(o1.cY - o2.cY, 2)); if (distance == Math.abs(o1.r - o2.r)) { System.out.println('E'); } else if (distance < Math.abs(o1.r - o2.r)) { System.out.println('I'); } else { System.out.println('O'); } } } private static TestCase[] readTestCases() { TestCase[] testCases = new TestCase[0]; BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); try { int n = Integer.parseInt(reader.readLine().trim()); testCases = new TestCase[n]; int index = 0; while (index < n) { String[] coord = reader.readLine().trim().split(" "); testCases[index++] = new TestCase(new CircleCoord(Integer.parseInt(coord[0]), Integer.parseInt(coord[1]), Integer.parseInt(coord[2])), new CircleCoord(Integer.parseInt(coord[3]), Integer.parseInt(coord[4]), Integer.parseInt(coord[5]))); } } catch (Exception e) { e.printStackTrace(); } finally { try { reader.close(); reader = null; } catch (IOException e) { e.printStackTrace(); } } return testCases; } }
which uses additional classes but is faster than this simple solution:
import java.util.Scanner; public class Main{ /** * @param args */ public static void main(String[] args) { Scanner scan = new Scanner (System.in); int cases= scan.nextInt(); for (int i = 0; i < cases; i++){ double x1= scan.nextInt(); double y1= scan.nextInt(); double r1= scan.nextInt(); double x2= scan.nextInt(); double y2= scan.nextInt(); double r2= scan.nextInt(); double d= Math.sqrt((Math.pow(x1-x2,2)+Math.pow(y1-y2,2))); if (d==Math.abs(r1-r2)){ System.out.println("E"); } else if(d<=Math.abs(r1-r2)){ System.out.println("I"); } else { System.out.println("O"); } } } }
i wonder how this happens. There is an additional class, therefore additional objects in the first solution and some test case, whereas the second one has only the test case. could you explain this to me? since this is not the first time i see such long solutions with additional classes which runs faster than simply written programs. Please, explain what slows down the second solution.
Posts: 1
Participants: 1