Integer Inquiry(大数相加)

本文介绍了一个用于处理和计算超长整数求和的C++程序设计方法。该程序能够接收最多100行输入,每行包含一个不超过100位的非常长的整数,并输出这些整数的总和。文章通过具体示例展示了如何实现这一功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Problem Description
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<iostream>
#include<stdio.h>
#include<string.h>
using namespace std;
char c[200],d[200],e[200];
void sum(char a[],char b[])
{
int al=strlen(a)-1;
int bl=strlen(b)-1;
int cl=0;
while(al>=0&&bl>=0)
{
c[cl]=c[cl]+a[al]+b[bl]-'0'*2;
c[cl+1]=c[cl+1]+c[cl]/10;
c[cl]=c[cl]%10;
al--;
bl--;
cl++;
}
while(al>=0)
{
c[cl]=c[cl]+a[al]-'0';
c[cl+1]=c[cl+1]+c[cl]/10;
c[cl]=c[cl]%10;
al--;
cl++;
}
while(bl>=0)
{
c[cl]=c[cl]+b[bl]-'0';
c[cl+1]=c[cl+1]+c[cl]/10;
c[cl]=c[cl]%10;
bl--;
cl++;
}
if(c[cl]==0)
cl--;
for(int i=cl,j=0; i>=0; i--,j++)
{
c[i]=c[i]+'0';
d[j]=c[i];
//printf("%c",c[i]);
}
//printf("\n");
//for(int i=0; i<=cl; i++)
//{
//printf("%c",c[cl-i]+'0');
//}printf("\n");
//return c[c1];
}
int main()
{
char a[150][150];
int i=0,j,k;
while(scanf("%s",a[i])!=EOF)
{
if(strcmp(a[i],"0")==0)
break;
i++;
}
strcpy(e,a[0]);
//if(strcmp(a[0],"0")==0)
//return 0;
for(j=1; j<i; j++)
{
memset(c,0,200);
sum(e,a[j]);
strcpy(e,d);
}
printf("%s\n",e);
return 0;
}

 
 
 

转载于:https://www.cnblogs.com/-lgh/p/4957629.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值