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 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 <stdio.h>
3 #include <stdlib.h>
4 #include <algorithm>
5 #include <cstring>
6 #include <math.h>
7 #include <ctime>
8 using namespace std;
9 #define maxn 32768
10 void add(char a[],char b[],char back[])
11 {
12 int i,j,k,up,x,y,z,l;
13 char *c;
14 if (strlen(a)>strlen(b)) l=strlen(a)+2; else l=strlen(b)+2;
15 c=(char *) malloc(l*sizeof(char));
16 i=strlen(a)-1;
17 j=strlen(b)-1;
18 k=0;up=0;
19 while(i>=0||j>=0)
20 {
21 if(i<0) x=‘0‘; else x=a[i];
22 if(j<0) y=‘0‘; else y=b[j];
23 z=x-‘0‘+y-‘0‘;
24 if(up) z+=1;
25 if(z>9) {up=1;z%=10;} else up=0;
26 c[k++]=z+‘0‘;
27 i--;j--;
28 }
29 if(up) c[k++]=‘1‘;
30 i=0;
31 c[k]=‘\0‘;
32 for(k-=1;k>=0;k--)
33 back[i++]=c[k];
34 back[i]=‘\0‘;
35 }
36
37 int main()
38 {
39 int n;
40 scanf("%d",&n);
41 char a[maxn]="0";
42 while(n--)
43 {
44 char b[maxn],c[maxn];
45 memset(b,‘\0‘,sizeof(b));
46 memset(c,‘\0‘,sizeof(c));
47 memset(a,‘\0‘,sizeof(a));
48 a[0]=‘0‘;
49 while(1)
50 {
51 scanf("%s",b);
52 if(b[0]==‘0‘)
53 break;
54 add(a,b,c);
55 strcpy(a,c);
56 memset(b,‘\0‘,sizeof(b));
57 memset(c,‘\0‘,sizeof(c));
58 }
59 if(n==0)
60 printf("%s\n",a);
61 else
62 printf("%s\n\n",a);
63
64 }
65 return 0;
66 }

hdu acm-1047 Integer Inquiry(大数相加),布布扣,bubuko.com

时间: 2024-12-21 00:52:01

hdu acm-1047 Integer Inquiry(大数相加)的相关文章

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

POJ 1503 Integer Inquiry(大数相加,java)

题目 我要开始练习一些java的简单编程了^v^ import java.io.*; import java.util.*; import java.math.*; public class Main { /** * @param args */ public static void main(String[] args) throws Exception { // 定义并打开输入文件 Scanner cin = new Scanner(System.in); BigInteger a, sum

POJ 1053 Integer Inquiry &amp;&amp; HDOJ 1047 Integer Inquiry (大数加法)

题目链接(POJ):http://poj.org/problem?id=1503 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1047 Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30856   Accepted: 12007 Description One of the first users of BIT's new su

Integer Inquiry(大数相加)

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

Integer Inquiry(大数相加)

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 on

HDUJ 1047 Integer Inquiry

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

HDU 1047.Integer Inquiry【多个大数相加】【高精度】【8月25】

Integer Inquiry 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,

HDU - 1047 - Integer Inquiry (大数高精度)

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

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 ex