大数加减乘法(Java)

A + B Problem II
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

如果用C语言或者C++都需要用数组去模拟竖式,特别的恶心,代码一撸就是几十行,挺恶心的。

#include <stdio.h>  
#include <string.h>  

char a[1001];  
char b[1001];  
char c[1001];  

void swap(char target[]) {  
    int i;  
    int j;  
    char temp;  

    for(i = 0,j = strlen(target) - 1;i <= j;i++,j--) {  
        temp = target[i];  
        target[i] = target[j];  
        target[j] = temp;  
    }  
}  

void add(char a[],char b[]) {  
    int i;  

    for(i = 0;i < strlen(a) && i < strlen(b);i++) {  
        c[i] += a[i] + b[i] - '0';  
        if(c[i] - '0' >= 10){  
            c[i] = c[i] - 10;  
            c[i+1] = 1;  
        }  
    }  
    //先将对其的进行运算  
    if(strlen(a) == strlen(b)) {  
        if(c[i] == 1)   
            c[i]='1';  
    }  
    //长度相等,看是否进一位  
    if(strlen(a) > strlen(b)){  
        if(c[i] == 1) {//进位的话就得每位进行累加  
            for(;i < strlen(a);i++){  
                c[i] += a[i];  
                if(c[i] - '0' >= 10) {  
                    c[i] = c[i] - 10;  
                    c[i+1] = 1;  
                }  
            }     

            if(c[i-1] == '0')//最后一位进位  
                c[i] = '1';  
        }  
        else { //不进位的话就直接把数组接在后面  
            for(;i < strlen(a);i++)  
                c[i] = a[i];  
        }  
    }  

    if(strlen(b) > strlen(a)){  
        if(c[i]==1){  
            for(;i < strlen(b);i++){  
                c[i] += b[i];  
                if(c[i] - '0' >= 10){  
                    c[i] = c[i] - 10;  
                    c[i+1] = 1;  
                }  
            }  
            if(c[i] == 1)  
                c[i] = '1';  
        } else {   
            for(;i < strlen(b);i++)  
                c[i] = b[i];  
        }  
    }  
}  

int main(void) {  
    scanf("%s",a);  
    scanf("%s",b);  
    swap(a);  
    swap(b);  
    add(a,b);  
    swap(c);  

    printf("%s\n",c);  

    return 0;  
}  

后来看一个同学比赛的比赛时一道相似题的题解http://blog.csdn.net/hepangda/article/details/73431191

才知道Java的BigInteger做这种题特别轻松,而且不需要担心溢出

import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        int T;
        int count = 1;
        Scanner cin = new Scanner(System.in);
        T = cin.nextInt();
        while(T-- > 0) {
            String s1 = cin.next();
            String s2 = cin.next();
            BigInteger a = new BigInteger(s1);
            BigInteger b = new BigInteger(s2);
            b = a.add(b);
            System.out.println("Case " + count + ":");
            System.out.println(s1 + " + " + s2 + " = " + b);
            count++;
            if(T > 0) {
                System.out.println();
            }
        }

        cin.close();
    }
}

对于大数加减乘法,无非就是调用bigInteger的add(),subtract(),multiply方法,但不能直接进行加减乘除,只能通过调用方法来完成


import java.math.BigInteger;
import java.util.Scanner;

public class Main {
    public static void main(String []args){
        Scanner cin = new Scanner(System.in);
        while(cin.hasNext()) {
            BigInteger a = cin.nextBigInteger();
            BigInteger b = cin.nextBigInteger();

            BigInteger c1 = a.add(b); //大数加法
            System.out.println("加的结果为 "+c1);

            BigInteger c2 = a.subtract(b); //大数减法
            System.out.println("减的结果为 "+c2);

            BigInteger c3 = a.multiply(b); //大数乘法
            System.out.println(c3);

            BigInteger c4 = a.divide(b); //大数除法
            System.out.println("除的结果为 "+c4);

            BigInteger c5 = a.mod(b); //大数模
            BigInteger c51 = a.remainder(b);
            System.out.println("模的结果为 "+c51);

            BigInteger c6 = a.max(b);//取最大
            System.out.println("最大为 "+c6);

            BigInteger c7 = a.min(b); //取最小
            System.out.println("最小为 "+c7);

            if(a.equals(b)) { //判断是否相等
                System.out.println("相等");
            }else {
                System.out.println("不相等");
            }

            BigInteger c8 = a.gcd(b); //求最大公约数
            System.out.println("最大公约数为 "+c8);
        }
        cin.close();
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值