请教一c++题目
ACM种有这么一题: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
我写了这么一个程序:
#include <iostream>
#include <string.h>
using namespace std;
char a[1001],b[1001];
char* add(char *a,char *b){
int i,j,k = 0,tmp[105],l1 = strlen(a),l2 = strlen(b);
for (i = l1-1,j = l2-1;i >= 0 && j >= 0;--i,--j)
tmp[k++] = a[i]+b[j]-'0'-'0';
for (;i >= 0;--i)
tmp[k++] = a[i]-'0';
for (;j >= 0;--j)
tmp[k++] = b[j]-'0';
tmp[k] = 0;
for (i = 0;i < k;++i){
tmp[i+1] += tmp[i]/10;
tmp[i] %= 10;
}
if (!tmp[k])
--k;
for (i = 0;i <= k;++i)
a[i] = tmp[k-i] + '0';
a[k+1] = '\0';
return a;
}
int main(){
char *r;
int t;
int i;
cin>>t;
if(t>=1&&t<=20)
{
for(i=1;i<=t;i++)
{
scanf("%s%s",a,b);
r = add(a,b);
cout<<"Case "<<i<<":"<<endl;
cout<<a<<" + "<<b<<" = "<<r<<endl;
if(i!=t)
cout<<endl;
}
}
return 0;
}
但提交时出现了以下错误:
Runtime Error
(ACCESS_VIOLATION
哪位大虾帮我下,不知道还有什么好的方法解决这题,快疯了。。。