杭电oj-1047-Integer Inquiry

来源:互联网 发布:什么叫大数据产业 编辑:程序博客网 时间:2024/06/05 16:47
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
题目意思是给你一个数n,代表有n个测试样例,每个测试样例给你好多行大数,当遇到一行单独为0时,
表示这组测试样例结束,让你输出这些大数之和,每两个测试样例答案之间有一个空行。
思路:这是一道简单的大数相加题,题目的坑点在于这两个样例
输入:          输入:
1                    1 
00                  0
00                  输出
0                    0
输出
0
只要注意这点就行了,代码如下:
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
using namespace std;
char f[250];
char f3[260];
int f1[250],f2[250];
char a[250],b[250];
void jia()
{
    int j;
    int l1=strlen(a);
    int l2=strlen(b);
    memset(f1,0,sizeof(f1));
    memset(f2,0,sizeof(f2));
    memset(f3,'\0',sizeof(f3));
    for(j=l1-1;j>=0;j--)
        f1[j]=a[l1-j-1]-'0';
    for(j=l2-1;j>=0;j--)
        f2[j]=b[l2-j-1]-'0';
    int l;
    int jinwei=0;
    if(l1>l2)
        l=l1;
    else
        l=l2;
    j=0;
    while(j<=l)
    {
        f1[j]=f1[j]+f2[j]+jinwei;
        if(f1[j]>9)
        {
            f1[j]-=10;
            jinwei=1;
        }
        else
            jinwei=0;
        j++;
    }
    while(f1[l]==0&&l)
        l--;
    for(j=l;j>=0;j--)
        f3[j]=f1[l-j]+'0';
    strcpy(a,f3);
    return;
}
int main()
{
    int n;
    scanf("%d",&n);
    while(n--)
    {
        scanf("%s",a);
        if(strcmp(a,"0")==0)
        {
            printf("0\n");
            if(n!=0)
                printf("\n");
            continue;
        }
        else
        {
            while(scanf("%s",b)&&strcmp(b,"0")!=0)
                jia();
            printf("%s\n",a);
        }
        if(n!=0)
                printf("\n");
    }
    return 0;
}
0 0