51nod 1005 大数加法 大数运算

15 篇文章 0 订阅
3 篇文章 0 订阅


基准时间限制:1 秒 空间限制:131072 KB 分值: 0  难度:基础题
 收藏
 关注
给出2个大整数A,B,计算A+B的结果。
Input
第1行:大数A
第2行:大数B
(A,B的长度 <= 10000 需注意:A B有可能为负数)
Output
输出A + B
Input示例
68932147586
468711654886
Output示例
537643802472
相关问题
大数乘法
0
 
大数开平方 
640
 
大数进制转换 
320
 
大数除法
320
 
大数乘法 V2
80

JAVA版1:

import java.util.*;
import java.io.*;
import java.lang.String;
import java.math.BigDecimal;
 
public class p1036
{
    public static void main(String[] args)
    {
        String s1,s2;
        Scanner cin = new Scanner(System.in);        
        s1 = cin.next();
        s2 = cin.next();
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2);
        System.out.println(b1.add(b2));
        cin.close();
    }
}


JAVA版2:

import java.io.PrintWriter;
import java.util.Scanner;

public class BigNumberPlus {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        PrintWriter out = new PrintWriter(System.out);
        
        String a = in.nextLine();
        String b = in.nextLine();
        
        boolean negtiveA = false;
        String aPositive = a;
        if (a.charAt(0) == '-') {
            negtiveA = true;
            aPositive = a.substring(1);
        }
        boolean negtiveB = false;
        String bPositive = b;
        if (b.charAt(0) == '-') {
            negtiveB = true;
            bPositive = b.substring(1);
        }
        String result;
        if (negtiveA == false && negtiveB == false) {
            result = plus(aPositive, bPositive);
        } else if (negtiveA == true && negtiveB == true) {
            result = plus(aPositive, bPositive);
            if (!(result.length() == 1 && result.charAt(0) == '0')) {
                result = "-" + result;
            }
        } else if (negtiveA == false && negtiveB == true) {
            result = substract(aPositive, bPositive);
        } else {
            result = substract(bPositive, aPositive);
        }

        out.println(result);
        out.flush();
        System.exit(0);
    }

    private static String plus(String num1, String num2) {
        StringBuilder sb = new StringBuilder();
        int index1 = num1.length() - 1;
        int index2 = num2.length() - 1;
        int carry = 0;
        while (index1 >= 0 || index2 >= 0 || carry > 0) {
            int value1 = index1 >= 0 ? num1.charAt(index1) - '0' : 0;
            int value2 = index2 >= 0 ? num2.charAt(index2) - '0' : 0;
            int valueSum = value1 + value2 + carry;
            carry = valueSum/10;
            valueSum %= 10;
            sb.append(valueSum);
            index1--;
            index2--;
        }
        return sb.reverse().toString();
    }

    private static String substract(String num1, String num2) {
        StringBuilder sb = new StringBuilder();
        boolean negtive = false;
        if (compare(num1, num2) < 0) {
            negtive = true;
            String temp = num1;
            num1 = num2;
            num2 = temp;
        }
        int index1 = num1.length() - 1;
        int index2 = num2.length() - 1;
        int carry = 0;
        while (index1 >= 0 || index2 >= 0) {
            int value1 = index1 >= 0 ? num1.charAt(index1) - '0' : 0;
            int value2 = index2 >= 0 ? num2.charAt(index2) - '0' : 0;
            int valueSum = value1 - carry - value2;
            carry = 0;
            if (valueSum < 0) {
                valueSum += 10;
                carry = 1;
            }
            sb.append(valueSum);
            index1--;
            index2--;
        }
        int firstNonZero = sb.length()-1;
        while (firstNonZero > 0 && sb.charAt(firstNonZero) == '0') {
            firstNonZero--;
        }
        sb = new StringBuilder(sb.substring(0, firstNonZero+1));
        if (negtive && !(sb.length() == 1 && sb.charAt(0) == '0')) {
            sb.append('-');
        }
        return sb.reverse().toString();
    }

    private static int compare(String num1, String num2) {
        if (num1.length() != num2.length()) {
            return num1.length() - num2.length();
        } else {
            for (int idx = 0; idx < num1.length(); idx++) {
                if (num1.charAt(idx) == num2.charAt(idx)) continue;
                return num1.charAt(idx) - num2.charAt(idx);
            }
        }
        return 0;
    }
}


C版:

#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       

char A[10005];
char B[10005];
int fa, fb;
void swap(char *a, int i, int j)
{
	char t = a[i]-'0';
	a[i] = a[j]-'0';
	a[j] = t;
}
void add(char *a, char *b)
{
	int la = strlen(a),lb = strlen(b);
	int i,j,c=0,s,l;
	for(i=fa,j=la-1; i<=j; ++i,--j) swap(a,i,j);
	for(i=fb,j=lb-1; i<=j; ++i,--j) swap(b,i,j);
	for(i=fa; i
       
       
        
        
          =fa; --i) printf("%d", a[i]); } int cmp(char *a, char *b) { int i,j,la,lb; la = strlen(a); lb = strlen(b); if(la-fa>lb-fb) return 1; else if(la-fa 
         
           b[i+fb]; } } void minus(char *a, char *b) { char *t; int i,j,ft,la,lb,c,l,s; if(!cmp(a,b)) { t=a; a = b; b = t; ft = fa; fa = fb; fb = ft; } la = strlen(a); lb = strlen(b); for(i=fa,j=la-1; i<=j; ++i,--j) swap(a,i,j); for(i=fb,j=lb-1; i<=j; ++i,--j) swap(b,i,j); c = 0; l = -1; for(i=0; i+fa 
          
            =0 ? 0 : 1; a[i+fa] = (10+a[i+fa]-b[i+fb]-c)%10; l = a[i+fa] ? i+fa : l; c = s; } if(l<0) printf("0"); else { if(fa) printf("-"); for(i=l; i>=fa; --i) printf("%d", a[i]); } } int main() { scanf("%s%s", A, B); fa = ('-'==A[0]); fb = ('-'==B[0]); if(fa^fb) minus(A,B); else add(A,B); } 
           
          
        
       
       
      
      
     
     
    
    





评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值