Integer Inquiry(大数加法)

Description

Download as PDF


 Integer Inquiry 

One of the first users of BIT's new supercomputer was Chip Diller. He extended his exploration of powers of 3 to go from 0 to 333 and he explored taking various sums of those numbers.

``This supercomputer is great,'' remarked Chip. ``I only wish Timothy were here to see these results.'' (Chip moved to a new apartment, once one became available on the third floor of the Lemon Sky apartments on Third Street.)

Input

The input will consist of at most 100 lines of text, each of which contains a single VeryLongInteger. Each VeryLongInteger will be 100 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).

The final input line will contain a single zero on a line by itself.

Output

Your program should output the sum of the VeryLongIntegers given in the input.

Sample Input

123456789012345678901234567890
123456789012345678901234567890
123456789012345678901234567890
0

Sample Output

370370367037037036703703703670
 
 

解题报告

大数快恶心死我了。。。

借用白书里面关于忽略前导零的代码

for(j = 105; j >= 0; j--) if(a[j]) break; for(i = j; i >= 1; i--) printf("%d", a[i]);


#include <stdio.h>
#include <string.h>
char str[100];
int n[100];
int a[105];
int main()
{
    int i,l,f,c,s,j;
    memset(a, 0, sizeof(a));
    while(scanf("%s", str))
    {
        if(str[0]=='0')
        break;
        l=strlen(str);
        memset(n, 0, sizeof(n));
        for(i=0;i<l;i++)
            n[l-i]=str[i]-'0';
        for(i=1;i<=l;i++)
            a[i]+=n[i];
        for(i=1; i<105; i++)
        {
            a[i]+=(a[i-1]/10);
            a[i-1]%=10;
        }
    }
    for(j = 105; j >= 0; j--) if(a[j]) break;
    for(i = j; i >= 1; i--) printf("%d", a[i]);
    printf("\n");
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java的栈数据结构实现大数加法的代码: ``` import java.util.Stack; public class LargeNumberAddition { public static String add(String num1, String num2) { Stack<Integer> stack1 = new Stack<>(); Stack<Integer> stack2 = new Stack<>(); Stack<Integer> resultStack = new Stack<>(); for (int i = 0; i < num1.length(); i++) { stack1.push(num1.charAt(i) - '0'); } for (int i = 0; i < num2.length(); i++) { stack2.push(num2.charAt(i) - '0'); } int carry = 0; while (!stack1.isEmpty() || !stack2.isEmpty()) { int sum = carry; if (!stack1.isEmpty()) { sum += stack1.pop(); } if (!stack2.isEmpty()) { sum += stack2.pop(); } resultStack.push(sum % 10); carry = sum / 10; } if (carry != 0) { resultStack.push(carry); } StringBuilder sb = new StringBuilder(); while (!resultStack.isEmpty()) { sb.append(resultStack.pop()); } return sb.toString(); } public static void main(String[] args) { String num1 = "123456789"; String num2 = "987654321"; String sum = add(num1, num2); System.out.println(num1 + " + " + num2 + " = " + sum); } } ``` 在这个实现中,我们首先将两个大数转化为栈,然后从栈顶开始逐位相加。由于我们不能直接对字符串进行加法运算,所以我们需要将每个字符转化为数字。我们使用ASCII码将字符转化为数字,例如'0'的ASCII码是48,所以'0'-'0'的结果是0。 我们使用一个carry变量来记录进位,初始值为0。在每次相加时,我们将carry加上栈顶元素,如果栈非空,则将栈顶元素弹出并加到sum中。我们将sum % 10的结果压入结果栈中,并将sum / 10的结果赋给carry。最后,如果carry不为0,则将其压入结果栈中。 最后,我们使用StringBuilder将结果栈中的数字拼接起来,形成最终结果。在本例中,输出结果为: 123456789 + 987654321 = 1111111110
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值