杭电oj-1047-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. ``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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值