BigNums——HDUOJ 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表示此次输入结束。
计算0以前若干行的大数和,并且输出。
共n行输出,每两行输出之间有一个空行(最后一行不用有空行!!)。

解题思路:

  1. 和A+B的思路一样,定义两个字符数组来保存按字符输入的两个加数。
  2. 使字符串的中的字符数字减去’0’,再将两个数字加上进位的数字>=10的可以使本位减10,下一位自增1。
  3. 注意输出格式!!!!(特别是0的时候,也要有输出一个0)。
* 提供几个测试数据:*

1
0

1
9999
99

1
99
9999

代码:

方法1:int数组存大数:
#include <stdio.h>
#include <string.h>
int sum[101];       //总数
int AllLength;      //总数的长度
int Add(char* num)
{
    int len1 = strlen(num) - 1; //加上的数的长度
    int sumIndex = 0;
    while (len1 >= 0)
    {
        if ((sum[sumIndex] + (num[len1] - '0')) >= 10)
        {
            sum[sumIndex] = (sum[sumIndex] + (num[len1] - '0')) - 10;
            sum[sumIndex + 1]++;
        }else{
            sum[sumIndex] = sum[sumIndex] + (num[len1] - '0');
        }
        sumIndex++;
        len1--;
    }
    while (sum[sumIndex] >= 10)//数目加完,继续进位!
    {
        sum[sumIndex] = sum[sumIndex] - 10;
        sum[sumIndex + 1]++;
        sumIndex++;//注意!!
    }
    if (sum[sumIndex] == 0)sumIndex--;;//判断最后一次进位时候还有向前进1

    AllLength = AllLength > sumIndex ? AllLength : sumIndex;//每次加完,获取数目的总长度

    return AllLength;
}

int main()
{
    int n;      //测试总数
    int IsNullScanf = true;
    char num1[101];
    scanf("%d", &n);
    while(n--)
    {   
        memset(sum, 0, sizeof(sum));
        AllLength = 0;

        while (scanf("%s",&num1)!=EOF && num1[0] != '0')
        {
            IsNullScanf = false;
            AllLength = Add(num1);
            memset(num1, '\0', sizeof(num1));
        }
        //输出
        for (; AllLength >= 0; AllLength--)
        {
            printf("%d", sum[AllLength]);
        }

        if (IsNullScanf != true)//判断输入是否不为空
        {       
            printf("\n");
        }else
        {
            printf("\n");
        }

        if (n > 0)
        {
            printf("\n");
        }

        IsNullScanf = true;
    }
}
方法2:string存储大数(高精度模板):

参考:http://www.cnblogs.com/kuangbin/archive/2012/08/13/2635653.html

#include<stdio.h>
#include<string>
#include<iostream>
using namespace std;

//高精度加法
//只能是两个正数相加
string add(string str1,string str2)//高精度加法
{
    string str;

    int len1=str1.length();
    int len2=str2.length();
    //前面补0,弄成长度相同
    if(len1<len2)
    {
        for(int i=1;i<=len2-len1;i++)
           str1="0"+str1;
    }
    else
    {
        for(int i=1;i<=len1-len2;i++)
           str2="0"+str2;
    }
    len1=str1.length();
    int cf=0;
    int temp;
    for(int i=len1-1;i>=0;i--)
    {
        temp=str1[i]-'0'+str2[i]-'0'+cf;
        cf=temp/10;
        temp%=10;
        str=char(temp+'0')+str;
    }
    if(cf!=0)  str=char(cf+'0')+str;
    return str;
}


int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        string sum="0";
        string str1;
        while(cin>>str1)
        {
            if(str1=="0")break;
            sum=add(sum,str1);
        }
        cout<<sum<<endl;
        if(T>0)cout<<endl;
    }
    return 0;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值