View Code of Problem 132


import java.util.Scanner;


public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {

            String s1 = scanner.next();
            String s2 = scanner.next();
            int max1 = getMax1(s1, s2) ;
            int a1[] = new int[max1 ];
            int b1[] = new int[max1 ];
            int max2 = getMax2(s1, s2) ;
            int a2[] = new int[max2 ];
            int b2[] = new int[max2 ];
            putIn1(a1, s1);
            putIn1(b1, s2);
            putIn2(a2, s1);
            putIn2(b2, s2);
            int carry = 0;
            int index = -1;
            for (int i = max2 - 1; i >= 0; i--) {
                int temp = a2[i] + b2[i] + carry;
                carry = 0;
                if (temp >= 10) {
                    carry = 1;
                }
                a2[i] = temp % 10;
                if (a2[i] != 0 && index == -1) {
                    index = i;
                }
            }
            for (int i = max1 - 1; i >= 0; i--) {
                int temp = a1[i] + b1[i] + carry;
                carry = 0;
                if (temp >= 10) {
                    carry = 1;
                }
                a1[i] = temp % 10;

            }
            if (carry==1){
                System.out.print("1");
            }
            for (int i = 0; i < max1; i++) {
                System.out.print(a1[0]);
            }
            if (index != -1) {
                System.out.print(".");
                for (int i = 0; i <= index; i++) {
                    System.out.print(a2[i]);
                }


            }
            System.out.println();
        }
    }

    public static void putIn1(int a[], String s) {
        String[] split = s.split("\\.");
        int length = 0;
        length = split[0].length();
        for (int i = 0; i < length; i++) {
            a[i ] = Integer.parseInt(split[0].charAt(i) + "");
        }
    }

    public static void putIn2(int a[], String s) {
        String[] split = s.split("\\.");
        int length = 0;
        if (split.length > 1) {
            length = split[1].length();
        }
        for (int i = 0; i < length; i++) {
            a[i] = Integer.parseInt(split[1].charAt(i) + "");
        }
    }

    public static int getMax1(String s1, String s2) {
        int length1 = 0, length2 = 0;
        String[] split1 = s1.split("\\.");
        String[] split2 = s2.split("\\.");
        length1 = split1[0].length();
        length2 = split2[0].length();
        return length1 > length2 ? length1 : length2;
    }

    public static int getMax2(String s1, String s2) {
        int length1 = 0, length2 = 0;
        String[] split1 = s1.split("\\.");
        String[] split2 = s2.split("\\.");
        if (split1.length > 1) {
            length1 = split1[1].length();
        }
        if (split2.length > 1) {
            length2 = split2[1].length();
        }

        return length1 > length2 ? length1 : length2;
    }
}

Double click to view unformatted code.


Back to problem 132