首页 > 试题广场 >

Powerful Calculator

[编程题]Powerful Calculator
  • 热度指数:6559 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
    Today, facing the rapid development of business, SJTU recognizes that more powerful calculator should be studied, developed and appeared in future market shortly. SJTU now invites you attending such amazing research and development work.     In most business applications, the top three useful calculation operators are Addition (+), Subtraction (-) and Multiplication (×) between two given integers. Normally, you may think it is just a piece of cake. However, since some integers for calculation in business application may be very big, such as the GDP of the whole world, the calculator becomes harder to develop.     For example, if we have two integers 20 000 000 000 000 000 and 4 000 000 000 000 000, the exact results of addition, subtraction and multiplication are:     20000000000000000 + 4000000000000000 = 24 000 000 000 000 000     20000000000000000 - 4000000000000000 = 16 000 000 000 000 000     20000000000000000 × 4000000000000000 = 80 000 000 000 000 000 000 000 000 000 000     Note: SJTU prefers the exact format of the results rather than the float format or scientific remark format. For instance, we need "24000000000000000" rather than 2.4×10^16.     As a programmer in SJTU, your current task is to develop a program to obtain the exact results of the addition (a + b), subtraction (a - b) and multiplication (a × b) between two given integers a and b.

输入描述:
   Each case consists of two separate lines where the first line gives the integer a and the second gives b (|a| <10^400 and |b| < 10^400).


输出描述:
    For each case, output three separate lines showing the exact results of addition (a + b), subtraction (a - b) and multiplication (a × b) of that case, one result per lines.
示例1

输入

20000000000000000
4000000000000000

输出

24000000000000000
16000000000000000
80000000000000000000000000000000
import java.util.Scanner;

import java.math.BigInteger;
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (in.hasNextBigInteger()) {
            BigInteger a = in.nextBigInteger();
            BigInteger b = in.nextBigInteger();
            System.out.println(a.add(b));
            System.out.println(a.subtract(b));
            System.out.println(a.multiply(b));
        }
    }
}

发表于 2023-03-10 12:16:56 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        String a = in.next();
        String b = in.next();
        System.out.println(add(a, b));
        System.out.println(sub(a, b));
        System.out.println(mult(a, b));
    }

    private static String add(String a, String b) {
        int n = a.length() - 1, m = b.length() - 1, add = 0;
        StringBuffer ans = new StringBuffer();
        while (n >= 0 || m >= 0 || add != 0) {
            int x = n >= 0 ? a.charAt(n) - '0' : 0;
            int y = m >= 0 ? b.charAt(m) - '0' : 0;
            int result = x + y + add;
            ans.append(result % 10);
            add = result / 10;
            n--;
            m--;
        }
        ans.reverse();
        return ans.toString();
    }

    private static String sub(String a, String b) {
        boolean flag = true;
        if (compare(a, b)) {
            flag = false;
            String tempStr = a;
            a = b;
            b = tempStr;
        }
        int n = a.length();
        int m = b.length();
        int carry = 0;
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < n; i++) {
            int temp = 0;
            if (i < m) {
                temp = a.charAt(n - i - 1) - '0' - (b.charAt(m - i - 1) - '0') - carry;
            } else {
                temp = a.charAt(n - i - 1) - '0' - carry;
            }
            if (temp < 0) {
                carry = 1;
                temp += 10;
            } else {
                carry = 0;
            }
            sb.append(temp);
        }
        while (sb.charAt(sb.length() - 1) == '0') {
            sb.deleteCharAt(sb.length() - 1);
        }
        if (!flag) {
            sb.append("-");
        }
        return sb.reverse().toString();
    }

    private static boolean compare(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        if (len1 < len2) {
            return true;
        } else if (len1 > len2) {
            return false;
        } else {
            int k = 0;
            while (k < len1) {
                char c1 = num1.charAt(k);
                char c2 = num2.charAt(k);
                if (c1 == c2) {
                    k++;
                    continue;
                }
                return c1 - c2 < 0;
            }
        }
        return false;
    }

    public static String mult(String num1, String num2) {
        if (num1.equals("0") || num2.equals("0")) {
            return "0";
        }
        String ans = "0";
        int m = num1.length(), n = num2.length();
        for (int i = n - 1; i >= 0; i--) {
            StringBuffer curr = new StringBuffer();
            int add = 0;
            for (int j = n - 1; j > i; j--) {
                curr.append(0);
            }
            int y = num2.charAt(i) - '0';
            for (int j = m - 1; j >= 0; j--) {
                int x = num1.charAt(j) - '0';
                int product = x * y + add;
                curr.append(product % 10);
                add = product / 10;
            }
            if (add != 0) {
                curr.append(add % 10);
            }
            ans = add(ans, curr.reverse().toString());
        }
        return ans;
    }
}
发表于 2023-02-13 17:07:42 回复(0)

问题信息

难度:
2条回答 4609浏览

热门推荐

通过挑战的用户

查看代码