Untitled

 avatar
unknown
java
a year ago
4.9 kB
6
Indexable
package src;

import java.util.Scanner;
import java.math.BigDecimal;
import java.math.RoundingMode;

public class HW2 {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        while (n-- > 0) {
            String v1 = scanner.next();
            String v2 = scanner.next();
            BigDecimal sum = add_func(v1, v2);

            String sumStringInt = sum.toString();
            sumStringInt = sumStringInt.split("\\.")[0]; // the first element after split
            BigDecimal sumBigDecimal = new BigDecimal(sumStringInt);

            if (sum.subtract(sumBigDecimal).doubleValue() == 0.9) { // if the recurring part is 0.9. ex: 4.9 will lead to the answer 5
                System.out.println(sum.intValue() + 1);
            } else if (sum.subtract(sumBigDecimal).doubleValue() == 0.0) {
                System.out.println(sum.intValue());
            } else {
                System.out.println(sum);
            }
        }
        scanner.close();
    }
    public static int lcm_func(int a, int b) {
        int max = Math.max(a, b);
        int min = Math.min(a, b);
        int lcm = max;
        while (true) {
            if (lcm % min == 0) {
                return lcm;
            }
            lcm += max;
        }
    }
    public static BigDecimal add_func(String str1, String str2) {
        int idx1 = str1.indexOf("."); // find the index of the period
        int idx2 = str2.indexOf(".");
        int len1 = str1.length() - idx1 - 1;
        int len2 = str2.length() - idx2 - 1;
        int lcm = lcm_func(len1, len2);
        int t1 = lcm / len1;
        int t2 = lcm / len2;

        if (str1.contains(".") == false || str2.contains(".") == false) { // 0.99 + 1 = 1.99
            BigDecimal num1 = new BigDecimal(str1);
            BigDecimal num2 = new BigDecimal(str2);
            BigDecimal answer = num1.add(num2);
            int cnt = 0;
            for (int i = 0; i < lcm; i++) {
                String answerString = answer.toString();
                int check0 = Integer.parseInt(String.valueOf(answerString.charAt(answerString.indexOf('.') + 1)));
                if (Integer.parseInt(String.valueOf(answerString.charAt(answerString.indexOf('.') + i + 1))) == check0) {
                    cnt++;
                }
                if (cnt == lcm) {
                    String tempString0 = answerString;
                    tempString0 = tempString0.split("\\.")[0];
                    tempString0 = tempString0.concat(".");
                    answerString = tempString0.concat(answerString.substring(answerString.indexOf('.') + 1, answerString.indexOf('.') + 2));
                    answer = new BigDecimal(answerString);
                    return answer;
                }
            }
            return answer;
        } else {
            String temp1 = str1; // change 1.2345 into 1.23452345, if lcm is 8, that is, t1 is 2
            for (int i = 0; i < 2 * t1 - 1; i++) {
                str1 = str1.concat(temp1.substring(idx1 + 1));
            }

            String temp2 = str2;
            for (int i = 0; i < 2 * t2 - 1; i++) {
                str2 = str2.concat(temp2.substring(idx2 + 1));
            }

            BigDecimal n1 = new BigDecimal(str1);
            BigDecimal n2 = new BigDecimal(str2);
            BigDecimal ans = n1.add(n2);

            // 3.56 + 9.2 -> 12.7878 12.78 / 6.8 + 0.142857 -> 7.031746031745 7.031746
            if ((Integer.parseInt(String.valueOf(str1.charAt(str1.indexOf('.') + lcm)))
                + Integer.parseInt(String.valueOf(str2.charAt(str2.indexOf('.') + lcm)))) > 9) { // if carry happened
                ans = ans.setScale(lcm, RoundingMode.DOWN);
            } else {
                ans = ans.setScale(lcm, RoundingMode.FLOOR);
            }

            int count = 0;
            for (int i = 0; i < lcm; i++) {
                String ansString = ans.toString();
                int check = Integer.parseInt(String.valueOf(ansString.charAt(ansString.indexOf('.') + 1))); // check is the first digit after period
                if (Integer.parseInt(String.valueOf(ansString.charAt(ansString.indexOf('.') + i + 1))) == check) {
                    count++;
                }
                if (count == lcm) {
                    String tempString = ansString;
                    tempString = tempString.split("\\.")[0];
                    tempString = tempString.concat(".");
                    ansString = tempString.concat(ansString.substring(ansString.indexOf('.') + 1, ansString.indexOf('.') + 2)); // wrong
                    ans = new BigDecimal(ansString);
                    return ans;
                }
            }
            return (ans);
        }
    }

}
Editor is loading...
Leave a Comment