大数加法(HDU6225 Little Boxes )

Description
Little boxes made of ticky-tacky.

Little boxes.

Little boxes.

Little boxes all the same.

There are a green boxes, and b pink boxes.

And c blue boxes and d yellow boxes.

And they are all made out of ticky-tacky.

And they all look just the same.

Input
The input has several test cases. The first line contains the integer t (1 ≤ t ≤ 10) which is the total number of test cases.

For each test case, a line contains four non-negative integers a, b, c and d where a, b, c, d ≤ 2^{62}2
62
, numbers of green boxes, pink boxes, blue boxes and yellow boxes.

Output
For each test case, output a line with the total number of boxes.
Little boxes on the hillside.

Little boxes made of ticky-tacky.

Little boxes.

Little boxes.

Little boxes all the same.

There are a green boxes, and b pink boxes.

And c blue boxes and d yellow boxes.

And they are all made out of ticky-tacky.

And they all look just the same.

Input
The input has several test cases. The first line contains the integer t (1 ≤ t ≤ 10) which is the total number of test cases.

For each test case, a line contains four non-negative integers a, b, c and d where a, b, c, d ≤ 2^{62}2
62
, numbers of green boxes, pink boxes, blue boxes and yellow boxes.

Output
For each test case, output a line with the total number of boxes.

题目大意
给定四个正整数a、b、c、d(小于2^62),计算其和。

思路
题目意思很简单,不难理解,就是计算四个数的和。
在我第一次写的时候没注意数的范围,用unsigned long long做的,直接挂了。这个题其实就是大数计算,直接用大数计算中加法就可以解决。

大数计算:
大数加法一般就是将两个大数存入两个字符数组内,将两个数组对应的每个数位进行相加,并进行相应的进位处理。
大数加法

#include <stdio.h>
#include <string.h>
int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        int i,j;
        int len;
        char a[101]={0},b[101]={0},c[101]={0},d[101]={0},ans[101]={0};
        char a_[101],b_[101],c_[101],d_[101];
        scanf("%s %s %s %s",a_,b_,c_,d_);//将数字先存入临时字符数组
        len=strlen(a_)>strlen(b_)?strlen(a_):strlen(b_);
        len=len>strlen(c_)?len:strlen(c_);
        len=len>strlen(d_)?len:strlen(d_);//找出最大数字长度
        /*将对应的各个数位字符减去'0',并倒位赋予新数组*/
        for(i=strlen(a_)-1,j=0;j<strlen(a_);i--,j++)
            a[j]=a_[i]-'0';
        for(i=strlen(b_)-1,j=0;j<strlen(b_);i--,j++)
            b[j]=b_[i]-'0';
        for(i=strlen(c_)-1,j=0;j<strlen(c_);i--,j++)
            c[j]=c_[i]-'0';
        for(i=strlen(d_)-1,j=0;j<strlen(d_);i--,j++)
            d[j]=d_[i]-'0';
        for(i=0;i<len;i++)
        {
            ans[i]+=a[i]+b[i]+c[i]+d[i];//对应数位相加
            if(ans[i]>9)//对于大于9的,进行进位处理
            {
                ans[i+1]=ans[i+1]+ans[i]/10;//向下一位进位
                ans[i]=ans[i]%10;//本位mod10
            }
        }
        if(ans[len]!=0)//确定最后的数字长度
            len++;
        for(i=len-1;i>=0;i--)//倒序输出
        {
            printf("%d",ans[i]);
        }
        printf("\n");
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值