Java之大数相加

之前参加某公司笔试,机试题目是大数相加,两大数是字符串形式,求和。

当时讨巧用的是BigDecimal类,但是发迷糊了,以为b1.add(b2)后,和就加到b1上了,结果一直输出不对。

其实应该是这样:

private static void add2Sum(String s1, String s2) {
        System.out.println("-----------");
        BigDecimal b1 = new BigDecimal(s1);
        BigDecimal b2 = new BigDecimal(s2)
       System.out.println("BigDecimal:"+b1.add(b2));
    }

但是,我觉得公司肯定不是考察这个用法的,应该是想让你自己写一个加法计算过程:

该思路是:

1.反转两个字符串,便于从低位到高位相加和最高位的进位导致和的位数增加;
2.对齐两个字符串,即短字符串的高位用‘0’补齐,便于后面的相加;
3.从头遍历,把两个正整数的每一位都相加,并加上进位;
4.最高位有进位则补上进位;
5.逆序输出;

在这里需要说明一点的是,string 类是没有reverse()方法的,所以,为了便利,在这里我们用了StringBuffer,可以使用它自带的reverse()方法,很方便。

public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String s1 = sc.nextLine();
        String s2 = sc.nextLine();

        //反转字符串
        String n1 = new StringBuffer(s1).reverse().toString();
        String n2 = new StringBuffer(s2).reverse().toString();
        int l1 = n1.length();
        int l2 = n2.length();
        int maxL = l1>l2?l1:l2;

        //补齐0
        if (l1 < l2) {
            for (int i = l1; i < l2; i++) {
                n1 += "0";
            }
        }else {
            for (int i = l2; i < l1; i++) {
                n2 += "0";
            }
        }
        //System.out.println(n1);//test
        //System.out.println(n2);//test
        StringBuffer res = new StringBuffer();//存放的结果
        int c = 0;//进位

        for (int i = 0; i < maxL; i++) {
            int nSum = Integer.parseInt(n1.charAt(i) + "") + Integer.parseInt(n2.charAt(i) + "") + c;
            int ap = nSum%10;
            res.append(ap);
            c = nSum/10;
        }
        if (c>0) {
            res.append(c);
        }
        //System.out.println(res);//test
        System.out.println(res.reverse());
    }

Over......

原文地址:https://www.cnblogs.com/gjmhome/p/11514861.html

时间: 2024-10-28 11:03:34

Java之大数相加的相关文章

java实现大数相加问题

闲来没事.写了个acm中常常遇到的大数加减问题的java 解决代码,我想说.用java的BigInteger 非常easy. 大爱java!! 比如: 实现多组输入的大数加减问题: import java.math.*; import java.util.*; public class Bignum { public static void main(String[] args) { BigInteger bigNumOne; BigInteger bigNumTwo; Scanner in =

hdu1250(Java)大数相加的问题

Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 10525 Accepted Submission(s): 3483 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequ

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -

POJ 1503 Integer Inquiry(大数相加,java)

题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) throws Exception { // 定义并打开输入文件 Scanner cin = new Scanner(System.in); BigInteger a, sum

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

HDU 1047。多个大数相加

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13496    Accepted Submission(s): 3390 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He e

栈的应用—大数相加

package org.Stone6762.MStack.adopt;    import java.util.Scanner;    import org.Stone6762.MStack.imple.LinkStack;    /**  * @author_Stone6762  * @Description_大数相加  * */  public class BigAdd {        /**      * @Describe_将表示数字的字符串从高位到低位的形式压入栈_并去除其中的空格 

ACM——大数相加

大数加法 时间限制(普通/Java):1000MS/3000MS          运行内存限制:65536KByte 总提交:2085            测试通过:543 描述 求两个非负整数(1000位以内)的和. 输入 两个非负整数(1000位以内),以空格分隔. 输出 两个非负整数的和. 样例输入 111111111111 222222222222 样例输出 333333333333 提示 题目来源 GUOJ #include<iostream> #include<strin

大数相乘和大数相加。

/* *大数相乘 求n的阶乘. */ import java.math.BigInteger;import java.util.Scanner; public class BigMultiply { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int N = sc.nextInt(); System.out.println(calcM(N)); } public static Stri