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

HDU 1047 多个大数相加

时间:2016-01-24 18:22:04      阅读:123      评论:0      收藏:0      [点我收藏+]

标签:

Integer Inquiry

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 16773    Accepted Submission(s): 4326


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. 


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.
 

 

Sample Input
1 123456789012345678901234567890 123456789012345678901234567890 123456789012345678901234567890 0
 

 

Sample Output
370370367037037036703703703670
 
 
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5 char sa[1000],sb[1000];
 6 int a[1000],b[1000],c[1000];
 7 void add(char sa[1000],char sb[1000]){
 8         int lena=strlen(sa);
 9         int lenb=strlen(sb);
10         int lenc=(lena>lenb?lena:lenb);
11         memset(a,0,sizeof(a));
12         memset(b,0,sizeof(b));
13 
14         for(int j=0;j<lena;j++){
15             a[lena-1-j]=sa[j]-0;
16         }
17         for(int j=0;j<lenb;j++){
18             b[lenb-1-j]=sb[j]-0;
19         }
20         memset(c,0,sizeof(c));
21         for (int ii=0; ii<lenc; ii++) {
22             c[ii]=a[ii]+b[ii]+c[ii];
23             if (c[ii]>=10) {
24                 c[ii+1]=1;
25                 c[ii]-=10;
26             }
27         }
28         if (c[lenc]>0) lenc++;
29 
30         for (int ii=lenc-1; ii>=0; ii--){
31             sb[ii]=c[lenc-1-ii]+0;
32         }
33         sb[lenc]=\0;
34 
35 }
36 int main() {
37     int n,l1,l2,k,maxn=0;
38     scanf("%d",&n);
39     while(n--) {
40         k=0;
41         strcpy(sb,"0");
42         while(scanf("%s",sa),strcmp(sa,"0")) {
43             k++;
44             add(sa,sb);
45         }
46         puts(sb);
47         //putchar(‘\n‘);
48 
49         if(n)
50             putchar(\n);
51     }
52     return 0;
53 }

 

HDU 1047 多个大数相加

标签:

原文地址:http://www.cnblogs.com/livelihao/p/5155483.html

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