类似于大数相加的一个题

Given an integer array of variable length like so [9, 8, 8, 3] where each item in array could be 0 to 9, write a function that would take would interpret the array [9, 8, 8, 3] as a number 9883 and increment it by 1. The return of the function would be an integer array containing the addition like so [9,8,8,4]. No zeros in the first position like [0,1,2,3]. I initially suggested a possible solution of process to convert the integer array to String then convert to Integer or Long and then do the addition of 1 and then convert it back to integer array. That is not allowed because the solution should be able to handle a very large number. Practice Tip: Remove the initial text, if any, in the G docs for more viewing room for code.

public static int[] bigAdd(int[] digits) {
    int currentDigitsSum = 1;
    for (int i=digits.length - 1; i > -1; i--) {
        currentDigitsSum = digits[i] + currentDigitsSum;
        digits[i] = currentDigitsSum % 10;
        if (currentDigitsSum /10 ==0) {
            return digits;
        } else {
            currentDigitsSum /= 10;
        }
    }
    if (currentDigitsSum != 0) {
        int[] newDigits = new int[digits.length + 1];
        System.arraycopy(digits, 0, newDigits, 1, digits.length);
        newDigits[0] = currentDigitsSum % 10;
        return newDigits;
    }
    return null;
}
时间: 2024-08-28 21:38:58

类似于大数相加的一个题的相关文章

高精度问题之大数相加(原来就是用字符串相加,模拟手算这么简单!)

解题心的: 就是基本的一对一模拟手算..借助c++的string 不用逆序运算了.很方便的补0.  最后处理下前导0的问题. #include <iostream> #include <string> using namespace std; // 实现大数相加 结果存放在num中 void bigIntergerAdd(string &num, string add) { int goBit = 0; // 存放进位 // 先交换下顺序 加数的位数要比较少 if (num

HDU 1250 Hat&#39;s Fibonacci(大数相加)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12952    Accepted Submission(s): 4331 Problem Description A Fibonacci sequence

C++实现string类型的大数相加(带小数)

近日,做了一道阿里给的大数相加的编程题.题目大意如下: 输入两个string类型的数,如12.223  11,判断输入字符串是否合法.合法则输出true以及相加结果(true 23.223),非法则输出false """". 期间几经修改,在判断合法方面排除了如.212以及122.这种错误(出现除数字以及.以外的错误亦已排除). 主要的思路是将小数与整数部分进行分离,分别相加.由于小数部分可能想整数部分进位,需要进行进位判断. 完整代码如下: #include &l

大数相加也算经典题之一了吧

大数相加 关于C语言大数(有千百位数的)问题,一般都是使用字符串来记录的.这里,将分享一下大数的代码. *分析 *就大数相加而言,首先末位对其,然后换成整数相加在加上进位 *记录进位,并对结果取模换成字符存入 *重复上述过程,直到公共部分加完 *然后把未加完的加进去 代码如下 1 char* BigSum(char *a,char*b)//传入的为两个大数 2 { 3 char* c=(char*)calloc(N+1,sizeof(char));//其中N为宏定义大数的最大位数 4 int l

大数相加(不开辟额外空间)

转载请注明出处:http://blog.csdn.net/ns_code/article/details/25555743 大数相加可以借助多种方法来实现,这里提供了一种链表节点的数据域为int型(用char型也可以,这样更省空间)的思路.这篇文章采用常用的转变为字符串进行处理的方法,下面说下我用字符串实现大数相加的思路: 假设输入了如下两个字符串(其中上面的红色部分表示数组的下标,下面的绿色和黄色部分表示各字符): s1: s2: 很明显,s1的实际长度为4,s2的实际长度为7,将二者在最低位

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

UVa 424 Integer Inquiry 【大数相加】

解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果.  Integer Inquiry  One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3

杭电ACM1297——Children’s Queue~~大数相加的应用

题目的意思很明确,不能单独有一个女生站一起. 假设有N个人. 1.最后一个人是男生,则有F(N - 1). 2.最后一个人是女生,则第N - 1也是女生,则有F(N - 1). 但还有一种就是,第N - 2个是女生,(是男生的话,包含在F(N - 1)中),但是第N - 3 个是男生,则不包含在上面的情况中,但是也是符合的.也就是最后三个是女生,倒数第四个是男生.也就是还有F(N - 4)种. 所以有递推公式:F(N) = F(N - 1) + F(N - 2) + F(N - 4). 下面的是

栈的应用—大数相加

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