UVa 424 - Integer Inquiry

超级计算机上的大数运算
本文介绍了一个使用超级计算机处理非常大的整数相加的问题。输入由若干行组成,每行包含一个大整数,程序需要计算这些整数的总和。文章提供了两种不同的实现方法来解决这个问题。


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
 
 
#include <stdio.h>
#include <string.h>
void plus(char n[],char s[],char m[])
{
    int i,j,k,len1,len2,r[600];
    memset(r,0,sizeof(int)*600);
    len1=strlen(n);
    len2=strlen(s);
    for(i=len1-1,k=599;i>=0;i--,k--)
        r[k]+=(n[i]-48);
    for(i=len2-1,k=599;i>=0;i--,k--)
        r[k]+=(s[i]-48);
    for(i=599;i>=0;i--)
        if(r[i]>9)
        {
            r[i-1]+=r[i]/10;
            r[i]%=10;
        }
    k=0;
    memset(m,0,sizeof(char)*600);
    while(r[k]==0&&k<=598)k++;
    for(i=k,j=0;i<=599;i++,j++)
    m[j]=r[i]+48;
}

int main()
{
    char s[600],n[600];
    scanf("%s",s);
    while(scanf("%s",n)!=EOF)
        {
            if(n[0]=='0')
            {
                printf("%s\n",s);
                memset(s,0,sizeof(char)*600);
            }
            else
            plus(n,s,s);
        }
    return 0;
}

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

void plus(char n[],char s[])
{
    int i,j,a,b;
    strrev(n);
    strrev(s);
    if(strlen(s)>strlen(n))
    {
        s[strlen(s)+1]='\0';
        s[strlen(s)]='0';
        for(i=strlen(n);i<strlen(s);i++)
        n[i]='0';
        n[i]='\0';
    }
    else
    {
        n[strlen(n)+1]='\0';
        n[strlen(n)]='0';
        for(i=strlen(s);i<strlen(n);i++)
        s[i]='0';
        s[i]='\0';
    }
    for(i=0,a=0;i<=strlen(n)-1;i++)
    {
        b=(int)s[i]-48+(int)n[i]-48+a;
        s[i]=b%10+48;
        a=b/10;
    }
    if(s[strlen(s)-1]=='0')
    s[strlen(s)-1]='\0';
    strrev(s);
}

int main()
{
    char s[105],n[105];
    scanf("%s",&s);
    while(scanf("%s",&n)!=EOF)
        {
            if(strcmp(n,"0")==0)
            {
                printf("%s\n",s);
                memset(s,0,sizeof(char)*600);
            }
            else
            plus(n,s);
        }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值