UVA_Integer Inquiry

A - Integer Inquiry

Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu

Submit Status

Description

 Integer Inquiry 

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

题意:给你n行大数;求它们的和;直接套用刘汝佳的模板;

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #define UNIT 10
 6
 7 using namespace std;
 8
 9 struct Bignum
10 {
11     int val[105];
12     int len;
13
14     Bignum ()
15     {
16         memset (val, 0, sizeof(val));
17         len = 1;
18     }
19
20     Bignum operator + (const Bignum &a) const//大数加大数
21     {
22         Bignum x = a;
23         int L;
24         L = a.len > len ? a.len : len;
25         for (int i = 0; i < L; ++i)
26         {
27             x.val[i] += val[i];
28             if (x.val[i] >= UNIT)
29             {
30                 x.val[i+1]++;
31                 x.val[i] -= UNIT;
32             }
33         }
34         if (x.val[L] != 0)
35             x.len = L+1;
36         else
37             x.len = L;
38         return x;
39     }
40
41 };
42 int main()
43 {
44    // freopen("ACM.txt","r",stdin);
45     char a[105];
46     Bignum x,ans;
47     for(;;)
48     {
49         Bignum(x);
50         scanf("%s",a);
51         int L=strlen(a);
52         x.len=0;
53         for(int i=L-1;i>=0;i--)
54         {
55             x.val[x.len++]=a[i]-‘0‘;
56         }
57         if(x.len==1&&x.val[0]==0) break;
58         ans=ans+x;
59
60     }
61     for(int i=ans.len-1;i>=0;i--)
62     cout<<ans.val[i];
63     cout<<endl;
64     return 0;
65 }

				
时间: 2024-11-10 17:54:21

UVA_Integer Inquiry的相关文章

hdu1047 Integer Inquiry

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

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

POJ 1053 Integer Inquiry (大数加法,还是Java大法好)

Integer Inquiry Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 32674   Accepted: 12789 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

杭电1407--Integer Inquiry

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

HDOJ 1047 Integer Inquiry

JAVA睑板.... Integer Inquiry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 12042    Accepted Submission(s): 3037 Problem Description One of the first users of BIT's new supercomputer was Chip D

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

CODEFORCES 645E Intellectual Inquiry

// CODEFORCES 645E Intellectual Inquiry http://codeforces.com/problemset/problem/645/E 字母表只有前面 k 个小写字母.给出一个字符串 s,你可以往 s 后添加 n 个字母,使得,总的字符串的不同子序列最多. Input n k s 1 ≤ n ≤ 1,000,000 1 ≤ k ≤ 26 1 ≤ |s| ≤ 1,000,000 Output 输出答案模 1,000,000,007. Solution 看了题解

UVa 424 Integer Inquiry 【大数相加】

解题思路:因为给定的数据是多组,所以我们只需要多次做加法就可以了,将上一次的和又作为下一次加法运算的一个加数. 反思:还是题意理解不够清楚,最开始以为只是算三个大数相加,后来才发现是多个,然后注意到当输入a的第一个字符为0的时候结束运算,输出结果.  Integer Inquiry  One of the firstusers of BIT's new supercomputer was Chip Diller. He extended his explorationof powers of 3

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