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, indicating the 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.
Sample Input
4
1 2 3 4
0 0 0 0
1 0 0 0
111 222 333 404
Sample Output
10
0
1
1070
题意:
就是给你四个数,让你输出这四个数的和;
思路
首先看数据范围:2^62;
首先肯定爆int,这就不用说了;然后看一下long long的范围:9223372036854775807(2^63-1);但是做的时候,第一遍就写的long long;然后wa; 原因是四个2 ^62相加就是2 ^64;也就是说这四个数的和最大就是2 ^64;爆了long long;然后我就用unsigned long long;(是long long的2倍**263-1**)当然也wa了;然后我就特判一下当这四个数都是262次方的时候,直接输出18446744073709551616
当然这属于卡数据的写法,题目测试数据改一下,可能就过不了了;
代码;
#include <bits/stdc++.h>
typedef unsigned long long ll;
using namespace std;
int main()
{
int t;
ll a,b,c,d;
ll dd=(ll)1<<62;//计算的时候右边会爆int 爆long long;所以要注意转化类型
scanf("%d",&t);
while(t--)
{
scanf("%llu %llu %llu %llu",&a,&b,&c,&d);
if(a==dd&&b==dd&&c==dd&&d==dd)
{
printf("18446744073709551616\n");
}
else
{
ll f=(ll)a+b+c+d;
printf("%llu\n",f);
}
}
return 0;
}
还是老老实实写一下大数相加吧;最近更新了大数相加的问题;大数相加相减(任意数高精度加减法)
代码:
#include <iostream>
using namespace std;
string add(string str1,string str2)
{
string str;
int len1=str1.length();
int len2=str2.length();
if(len1>len2)
for(int i=1;i<=len1-len2;i++)
str2='0'+str2;
else
for(int i=1;i<len2-len1;i++)
str1='0'+str1;
len1=str1.length();
int temp,carry=0;
for(int i=len1-1;i>=0;i--)
{
temp=str1[i]-'0'+str2[i]-'0'+carry;
carry=temp/10;
temp%=10;
str=char(temp+'0')+str;
}
if(carry!=0) str=char(carry+'0')+str;
return str;
}
int main()
{
string a,b,c,d;
int t;
cin >>t;
while(t--)
{
cin >>a>>b>>c>>d;
cout <<add(add(a,b),add(c,d))<<endl;
}
return 0;
}