Advertisement
Guest User

Spaceship

a guest
Feb 1st, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.37 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. class SpaceshipMomentum {
  5.     private static String V, M;
  6.     private static char[] temp;
  7.     private static char[] velocity;
  8.     private static char[] mass;
  9.  
  10.     public static void main(String[] args) {
  11. //      long startTime = System.nanoTime();
  12.  
  13.         Scanner sc = new Scanner(System.in);
  14.         PrintWriter pw = new PrintWriter(new BufferedWriter(
  15.                 new OutputStreamWriter(System.out)));
  16.  
  17.         int T, B;
  18.         String output;
  19.  
  20.         T = sc.nextInt();
  21.  
  22.         for (int i = 1; i <= T; ++i) {
  23.             B = sc.nextInt();
  24.             sc.nextLine();
  25.             V = sc.nextLine();
  26.             M = sc.nextLine();
  27. //          velocity = new char[V.length()];
  28. //          mass = new char[M.length()];
  29.  
  30.             if (V.equals("0") || M.equals("0")) {
  31.                 output = "0";
  32.             } else {
  33.                 velocity = V.toCharArray();
  34.                 mass = M.toCharArray();
  35.  
  36. //              preProcess(velocity, mass);
  37.  
  38.                 output = multiply(velocity, mass);
  39.             }
  40.  
  41.             pw.write(trimZeros(output));
  42.             pw.write("\n");
  43.         }
  44.  
  45. //      long endTime = System.nanoTime();
  46. //
  47. //      long elapsedTime = (endTime - startTime);
  48. //      double seconds = (double)elapsedTime / 1000000000.0;
  49. //
  50. //      pw.write("timing: ");
  51. //      pw.write("\n");
  52. //      pw.write(String.valueOf(seconds));
  53. //      pw.write("\n");
  54.         pw.close();
  55.     }
  56.  
  57.     /**
  58.      * Convert the input strings (velocity and mass) into ArrayList
  59.      */
  60.     private static void preProcess(char[] velocity, char[] mass) {
  61.         Arrays.fill(velocity, '0');
  62.         Arrays.fill(mass, '0');
  63.         int index = 0;
  64.  
  65.         for (int i = V.length(); i > 0; i--) {
  66.             velocity[index] = V.charAt(i - 1);
  67.             index++;
  68.         }
  69.  
  70.         index = 0;
  71.         for (int i = M.length(); i > 0; i--) {
  72.             mass[index] = M.charAt(i - 1);
  73.             index++;
  74.         }
  75.     }
  76.  
  77.     /**
  78.      * Multiply velocity and mass, then output the result
  79.      */
  80.     private static String multiply(char[] v, char[] m) {
  81.         String start = "1";
  82.         char[] reversedOutput = new char[V.length() + M.length() + 1];
  83.         String output = "";
  84.         Arrays.fill(reversedOutput, '0');
  85.  
  86.         for (int i = m.length; i > 0; i--) {
  87.             multiplication(v, m[i - 1], start);
  88.             addition(reversedOutput);
  89.             start = start.concat("0");
  90.         }
  91.  
  92.         for (int i = reversedOutput.length; i > 0; i--) {
  93.             output = output.concat(String.valueOf(reversedOutput[i - 1]));
  94.         }
  95.  
  96.         return output;
  97.     }
  98.  
  99.     /**
  100.      * Add the temporary multiplication result to the final output
  101.      */
  102.     private static void addition(char[] reversedOutput) {
  103.         int arg1, arg2;
  104.         int tempResult;
  105.         int carry = 0;
  106.         int index = 0;
  107.  
  108.         for (int i = 0; i < temp.length; i++) {
  109.             arg1 = parseDigit(reversedOutput[i]);
  110.             arg2 = parseDigit(temp[i]);
  111.             tempResult = arg1 + arg2 + carry;
  112.  
  113.             reversedOutput[i] = toDigit(tempResult % 10);
  114.  
  115.             carry = tempResult / 10;
  116.  
  117.             index = i;
  118.         }
  119.  
  120.         if (carry > 0) {
  121.             reversedOutput[index + 1] = toDigit(carry);
  122.         }
  123.     }
  124.  
  125.     /**
  126.      * Multiply each digit of mass with velocity, then output the result
  127.      */
  128.     private static void multiplication(char[] v, char c, String start) {
  129.         temp = new char[V.length() + start.length() + 1];
  130.         Arrays.fill(temp, '0');
  131.         int arg1 = parseDigit(c);
  132.         int carry = 0;
  133.         int index = start.length() - 1;
  134.  
  135.         for (int i = v.length; i > 0; i--) {
  136.             int arg2 = parseDigit(v[i - 1]);
  137.             int tempResult = arg1 * arg2;
  138.             tempResult += carry;
  139.             if (tempResult > 9) {
  140.                 carry = tempResult / 10;
  141.             } else {
  142.                 carry = 0;
  143.             }
  144.  
  145.             temp[index] = toDigit(tempResult % 10);
  146.             index++;
  147.         }
  148.  
  149.         if (carry != 0) {
  150.             temp[index] = toDigit(carry);
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Use to trim leading and trailing zeros on a result string.
  156.      */
  157.     private static String trimZeros(String input) {
  158.         int left = 0;
  159.         int right = input.length() - 1;
  160.         int fp = input.indexOf('.');
  161.         if (fp == -1) {
  162.             fp = input.length();
  163.         }
  164.  
  165.         while (left < fp - 1) {
  166.             if (input.charAt(left) != '0')
  167.                 break;
  168.             left++;
  169.         }
  170.  
  171.         while (right >= fp) {
  172.             if (input.charAt(right) != '0') {
  173.                 if (input.charAt(right) == '.')
  174.                     right--;
  175.                 break;
  176.             }
  177.             right--;
  178.         }
  179.  
  180.         if (left >= fp)
  181.             return "0" + input.substring(left, right + 1);
  182.         return input.substring(left, right + 1);
  183.     }
  184.  
  185.     /**
  186.      * Convert digit to int (for reading)
  187.      */
  188.     private static int parseDigit(char c) {
  189.         if (c <= '9') {
  190.             return c - '0';
  191.         }
  192.         return c - 'A' + 10;
  193.     }
  194.  
  195.     /**
  196.      * Convert int to digit. (for printing)
  197.      */
  198.     private static char toDigit(int digit) {
  199.         if (digit <= 9) {
  200.             return (char) (digit + '0');
  201.         }
  202.         return (char) (digit - 10 + 'A');
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement