Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 452533 Accepted Submission(s): 87724
Problem Description
I have a very simple problem for you. Given two integers A and B, your job is to calculate the Sum of A + B.
Input
The first line of the input contains an integer T(1<=T<=20) which means the number of test cases. Then T lines follow, each line consists of two positive integers, A and B. Notice that the integers are very large, that means you should not process them by using 32-bit integer. You may assume the length of each integer will not exceed 1000.
Output
For each test case, you should output two lines. The first line is “Case #:”, # means the number of the test case. The second line is the an equation “A + B = Sum”, Sum means the result of A + B. Note there are some spaces int the equation. Output a blank line between two test cases.
Sample Input
2
1 2
112233445566778899 998877665544332211
Sample Output
Case 1:
1 + 2 = 3
Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110
问题连接
A+B问题的进阶版。
与1000不同的是A和B可能太大了,无法用int,long long存储。所以换个做法,用字符串记录数字并输出。好处是可以记录非常大的数了,不好的是因为变成字符而不能直接计算,得先转换成数字在进行运算,这也是无法避免的。
先的对字符串处理:
方法一:我是用字符型数组做的。又做了两个函数,一个用于反转,一个用于相加。
反转函数:像"123456"变成"654321"。其实可以不用,第一次做的时候就顺应平时的对齐习惯。
相加函数:
方法二:用了下最近复习的string类型。发现string处理真的很方便!
赋值也是可以不用像字符型数组还得再建一个副本。
比较要注意的是:由于c语言没有string类型的变量,所以不能直接用printf()函数输出,但可以加个.c_str()函数,相当于转换为等价的字符型数组,就可以输出了。
方法一:
#include
#include
using namespace std;
char num1[1002], num2[1002], num3[1002];
void reversal(char*);
void add(char*, char*, char*);
int main()
{
int t, n;
cin >> t;
n = t;
while (t--)
{
memset(num1, '\0', 1002);//注意重置
memset(num2, '\0', 1002);//注意重置
memset(num3, '\0', 1002);//注意重置
cin >> num1 >> num2;
reversal(num1);
reversal(num2);
add(num1, num2, num3);
reversal(num1);
reversal(num2);
reversal(num3);
printf("Case %d:\n%s + %s = %s\n", n - t, num1, num2, num3);
if (t > 0) cout << endl;
}
system("pause");
return 0;
}
void reversal(char*p)
{
int len = strlen(p), i;
char t;
for (i = 0; i < len / 2; i++)
{
t = p[i];
p[i] = p[len - 1 - i];
p[len - 1 - i] = t;
}
}
void add(char*p1, char*p2, char*p3)
{
int am, bm, t, i, s;
for (i = t = 0; p1[i] != '\0' || p2[i] != '\0'; i++)
{
if (p1[i] == '\0') am = 0;
else am = p1[i] - '0';
if (p2[i] == '\0') bm = 0;
else bm = p2[i] - '0';
s = am + bm + t;
p3[i] = s % 10 + '0';
t = s / 10;
}
if (t != 0) //注意可能增多一位
p3[i] = t + '0';
}
方法二:
#include
#include
using namespace std;
int main()
{
int t, n;
cin >> t;
n = t;
while (t--)
{
string num1, num2, num3;
cin >> num1 >> num2;
bool state = true;
int len1 = num1.size(), len2 = num2.size(),i,j,s,am,bm;
if (len1 > len2)//让num1为长度小的
{
string num = num1;
num1 = num2;
num2 = num;
int len = len1;
len1 = len2;
len2 = len;
state = false;
}
num3 = num2;
for (i =1,j=0; i <=len2; i++)//与之前不同的是没有做调转函数
{
if (i > len1)am = 0;
else am = num1[len1 - i] - '0';
bm = num2[len2 - i] - '0';
s = am + bm + j;
num3[len2 - i] = s % 10+'0';
j = s / 10;
}
if (!state)
{
string num = num1;
num1 = num2;
num2 = num;
}
if (j > 0) printf("Case %d:\n%s + %s = %d%s\n", n - t, num1.c_str(), num2.c_str(), j, num3.c_str());
/*不能用printf()直接对string类型变量输出,要先添加个.c_str()*/
else printf("Case %d:\n%s + %s = %s\n", n - t, num1.c_str(), num2.c_str(), num3.c_str());
if (t > 0)cout << endl;
}
system("pause");
return 0;
}