码迷,mamicode.com
首页 > 其他好文 > 详细

Integer Inquiry(大数相加)

时间:2015-11-11 23:40:37      阅读:235      评论:0      收藏:0      [点我收藏+]

标签:

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;
}

 
 
 

Integer Inquiry(大数相加)

标签:

原文地址:http://www.cnblogs.com/-lgh/p/4957629.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!