新手 关于HDUOJ大数数组相加的问题

彼德V 2017-11-24 10:03:15
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



Author
Ignatius.L


Recommend
We have carefully selected several similar problems for you: 1004 1003 1008 1005 1089
(就是用把超大数字用字符串储存在数组中,然后再转换为数组进行模拟十进制加减,但是我不知道为什么我的代码一旦输入两次以上的数据后面几次运算得到的结果就总会比实际结果小)
#include "stdafx.h"
#include<string.h>
#define MAXD 1010
char str1[MAXD];
char str2[MAXD];
int num1[MAXD];
int num2[MAXD];
int num3[MAXD];
int main()
{
memset(str1, 0, sizeof(str1));
memset(str2, 0, sizeof(str2));
memset(num1, 0, sizeof(num1));
memset(num2, 0, sizeof(num2));
memset(num3, 0, sizeof(num3)); //置0
int n;
int i;

int j = 0;
int k = 0;
scanf_s("%d", &n);
for (int n2 = 1; n2 <= n; n2++)
{
scanf_s("%s %s", str1, MAXD, str2, MAXD);
int len1 = strlen(str1);
int len2 = strlen(str2);
int max = (len1 > len2) ? len1 : len2; //取两者位数大的

for (i = len1 - 1; i >= 0; i--) //把数组1转换为int类型
{
num1[j++] = (str1[i] - '0');
}
j = 0;
for (i = len2 - 1; i >= 0; i--) //把数组2转换为int类型
{
num2[j++] = (str2[i] - '0');
}
for (i = 0; i <= max - 1; i++) //数组1+数组2
{
num2[i] += num1[i];
if (num2[i] >= 10)
{
num2[i] -= 10;
num2[i + 1]++;;
}
}
if (num2[max])
printf("%d", num2[max]);
printf("Case %d:\n", n);
printf("%s ", str1);
printf("+");
printf(" %s ", str2);
printf("= ");
for (i = max - 1; i >= 0; i--)
{

printf("%d", num2[i]);
k++;
}
printf("\n");
}
return 0;
}
谢谢各位
...全文
165 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
paschen 2017-11-24
  • 打赏
  • 举报
回复
必要的位置设置断点,然后单步跟踪程序运行,观察变量变化情况,分析原因

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧