【九度】题目1037:Powerful Calculator

题目1037:Powerful Calculator
时间限制:1 秒内存限制:32 兆特殊判题:否提交:1397解决:392
题目描述:
    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.
样例输入:
20000000000000000
4000000000000000
样例输出:
24000000000000000
16000000000000000
80000000000000000000000000000000
来源:
2007年上海交通大学计算机研究生机试真题
答疑:
解题遇到问题?分享解题心得?讨论本题请访问: http://t.jobdu.com/thread-7761-1-1.html
【解题思路】
大数据加减乘,可以转换为数组计算。
加法和乘法其实是一样的,注意位数就可以了,之前的文章中对次有说明,可以参考。
减法注意的是,大数减小数,用compareTo比较大小即可。最后不要忘记符号位。
本题测试用例没有考虑负数,所以本题的代码只针对正数操作。如果遇到有负数的,可以自行扩展。
1、转为BigInteger,直接计算。

Java AC

import java.math.BigInteger;
import java.util.Scanner;
 
public class Main{
    /*
     * 1037
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String a = scanner.next();
            String b = scanner.next();
            BigInteger biga = new BigInteger(a);
            BigInteger bigb = new BigInteger(b);
            System.out.println(biga.add(bigb).toString());
            System.out.println(biga.subtract(bigb));
            System.out.println(biga.multiply(bigb));
        }
    }
}
/**************************************************************
    Problem: 1037
    User: wzqwsrf
    Language: Java
    Result: Accepted
    Time:90 ms
    Memory:15628 kb
****************************************************************/
2、转为数组计算。

Java AC

import java.util.Scanner;
 
public class Main {
    /*
     * 2014年5月13日12:20:08
     */
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNext()) {
            String num1 = scanner.next();
            String num2 = scanner.next();
            System.out.println(add(num1, num2));
            System.out.println(sub(num1, num2));
            System.out.println(multiply(num1, num2));
        }
    }
 
    public static String add(String num1, String num2) {
        int aLength = num1.length();
        int bLength = num2.length();
        int max = Math.max(aLength, bLength);
        int[] arrayA = new int[max + 1];
        int[] arrayB = new int[max + 1];
        int k = 0;
        for (int i = max + 1 - aLength; i < max + 1; i++) {
            arrayA[i] = num1.charAt(k) - '0';
            k++;
        }
        k = 0;
        for (int i = max + 1 - bLength; i < max + 1; i++) {
            arrayB[i] = num2.charAt(k) - '0';
            k++;
        }
        int[] arrayC = new int[max + 1];
 
        for (int i = max; i > 0; i--) {
            arrayC[i] += arrayA[i] + arrayB[i];
            if (arrayC[i] >= 10) {
                arrayC[i] -= 10;
                arrayC[i - 1] += 1;
            }
        }
        return printfResult(arrayC);
    }
 
    public static String sub(String num1, String num2) {
        int aLength = num1.length();
        int bLength = num2.length();
        int flag = compareStr(num1, num2);
        if (flag == 0) {
            return "0";
        }
        if (flag < 0) {
            String tempStr = num1;
            num1 = num2;
            num2 = tempStr;
        }
        aLength = num1.length();
        bLength = num2.length();
        int max = Math.max(aLength, bLength);
        int[] arrayA = new int[max + 1];
        int[] arrayB = new int[max + 1];
        int k = 0;
        for (int i = max + 1 - aLength; i < max + 1; i++) {
            arrayA[i] = num1.charAt(k) - '0';
            k++;
        }
        k = 0;
        for (int i = max + 1 - bLength; i < max + 1; i++) {
            arrayB[i] = num2.charAt(k) - '0';
            k++;
        }
        int[] arrayC = new int[max + 1];
        for (int i = max; i > 0; i--) {
            if (arrayA[i] < arrayB[i]) {
                arrayA[i - 1] -= 1;
                arrayA[i] += 10;
            }
            arrayC[i] += arrayA[i] - arrayB[i];
        }
        String result = printfResult(arrayC);
        result = flag < 0 ? "-" + result : result;
        return result;
    }
 
    private static int compareStr(String num1, String num2) {
        int len1 = num1.length();
        int len2 = num2.length();
        if (len1 > len2) {
            return 1;
        }
        if (len1 < len2) {
            return -1;
        }
        if (len1 == len2) {
            return num1.compareTo(num2);
        }
        return 0;
    }
 
    public static String multiply(String num1, String num2) {
        if ("".equals(num1.replace("0", ""))
                || "".equals(num2.replace("0", ""))) {
            return "0";
        }
        int len1 = num1.length();
        int len2 = num2.length();
        char arrayA[];
        char arrayB[];
        arrayA = num1.toCharArray();
        arrayB = num2.toCharArray();
        int column = len1 + len2;
        int arrayC[] = new int[column];
        int k = column;
        for (int i = len2 - 1; i >= 0; i--) {
            k--;
            int tempCol = k;
            int tempArr[] = new int[column];
            int num = arrayB[i] - '0';
            for (int j = len1 - 1; j >= 0; j--) {
                tempArr[tempCol] += (arrayA[j] - '0') * num;
                if (tempArr[tempCol] >= 10) {
                    int mod = tempArr[tempCol] / 10;
                    tempArr[tempCol] %= 10;
                    tempArr[tempCol - 1] += mod;
                }
                tempCol--;
            }
            arrayC = addNum(tempArr, arrayC, column);
        }
        return printfResult(arrayC);
    }
 
    private static String printfResult(int[] arrayC) {
        StringBuffer sb = new StringBuffer();
        int i = 0;
        if (arrayC[0] == 0) {
            i = 1;
        }
        for (; i < arrayC.length; i++) {
            sb.append(arrayC[i]);
        }
        return sb.toString();
    }
 
    public static int[] addNum(int tempArr[], int result[], int column) {
        for (int i = column - 1; i >= 0; i--) {
            result[i] += tempArr[i];
            if (result[i] >= 10) {
                result[i] -= 10;
                result[i - 1] += 1;
            }
        }
        return result;
    }
}
/**************************************************************
    Problem: 1037
    User: wangzhenqing
    Language: Java
    Result: Accepted
    Time:150 ms
    Memory:21048 kb
****************************************************************/


你可能感兴趣的:(【九度】题目1037:Powerful Calculator)