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");
}
}