HDU 1047 多个大数相加

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 }
时间: 2024-10-12 08:39:54

HDU 1047 多个大数相加的相关文章

HDU 1047 Integer Inquiry 大数相加 string解法

本题就是大数相加,题目都不用看了. 不过注意的就是HDU的肯爹输出,好几次presentation error了. 还有个特殊情况,就是会有空数据的输入case. #include <stdio.h> #include <vector> #include <string.h> #include <algorithm> #include <iostream> #include <string> #include <limits.h

hdu acm-1047 Integer Inquiry(大数相加)

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 11678    Accepted Submission(s): 2936 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He ex

HDU 1047。多个大数相加

Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13496    Accepted Submission(s): 3390 Problem Description One of the first users of BIT's new supercomputer was Chip Diller. He e

HDU 1250 Hat&#39;s Fibonacci(Java大数相加)+讲解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Problem Description A Fibonacci sequence is calculated by adding the previous two members the sequence, with the first two members being both 1. F(1) = 1, F(2) = 1, F(3) = 1,F(4) = 1, F(n>4) = F(n -

hdu 1250 Hat&#39;s Fibonacci (大数相加)

//a[n]=a[n-1]+a[n-2]+a[n-3]+a[n-4]; # include <stdio.h> # include <algorithm> # include <string.h> # include <iostream> using namespace std; int a[10000][260]={0}; //每个元素可以存储8位数字,所以2005位可以用260个数组元素存储. int main() { int i,j,n; a[1][0

HDU 1316 (斐波那契数列,大数相加,大数比较大小)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1316 Recall the definition of the Fibonacci numbers: f1 := 1 f2 := 2 fn := fn-1 + fn-2 (n >= 3) Given two numbers a and b, calculate how many Fibonacci numbers are in the range [a, b]. Input The input c

HDU 1250 Hat&#39;s Fibonacci(大数相加)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1250 Hat's Fibonacci Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12952    Accepted Submission(s): 4331 Problem Description A Fibonacci sequence

HDU 1297 Children’s Queue (递推、大数相加)

Children’s Queue Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 17918    Accepted Submission(s): 5976 Problem Description There are many students in PHT School. One day, the headmaster whose na

java-两个大数相加

题目要求:用字符串模拟两个大数相加. 一.使用BigInteger类.BigDecimal类 public static void main(String[] args) { String a="8888899999999888";  String b="88888888888888";  String str=new BigInteger(a).add(new BigInteger(b)).toString();  System.out.println(str);