# 题目信息

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 565951 Accepted Submission(s): 107979

# 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

1
2
3
2
1 2
112233445566778899 998877665544332211

# Sample Output

1
2
3
4
5
Case 1:
1 + 2 = 3

Case 2:
112233445566778899 + 998877665544332211 = 1111111111111111110

# 题解

# C 语言解法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>

int main() {

int n, i = 0;
scanf("%d", &n);
char num1[1001];
char num2[1001];
while (i < n) {
char res[1001];
int res_end_pos = 0;
int temp = 0;
int num1_end_pos = 0;
int num2_end_pos = 0;

scanf("%s %s", num1, num2);

// 将下标计算到数值的个位。
while ('\0' != num1[num1_end_pos]) {
num1_end_pos++;
}

while ('\0' != num2[num2_end_pos]) {
num2_end_pos++;
}

// 按照下标从大到小的顺序,累加,并计算是否需要进位。
res_end_pos = (num1_end_pos > num2_end_pos ? num1_end_pos : num2_end_pos) + 1;
res[res_end_pos] = '\0';
while (num1_end_pos > 0 && num2_end_pos > 0) {
num2_end_pos--;
num1_end_pos--;
res_end_pos--;

int number1 = num1[num1_end_pos] - '0';
int number2 = num2[num2_end_pos] - '0';
temp = number1 + number2 + (temp / 10);
res[res_end_pos] = (temp % 10) + '0';
}

// 计算进位以及某个数值位数不一致的情况
while (res_end_pos > 0) {
res_end_pos--;
int temp_res = 0;
if (num1_end_pos == 0 && num2_end_pos > 0) {
num2_end_pos--;
temp_res = (num2[num2_end_pos] - '0') + temp / 10;
} else if (num1_end_pos > 0 && num2_end_pos == 0) {
num1_end_pos--;
temp_res = (num1[num1_end_pos] - '0') + temp / 10;
} else {
temp_res = temp / 10;
}
temp = temp_res;
res[res_end_pos] = temp_res % 10 + '0';
}
i++;

// 处理没有进位导致的输出问题(多输出一个0)
if(res[0] == '0'){
int j=0;
while(res[j]!='\0'){
res[j] = res[j+1];
j++;
}
}

printf("Case %d:\n", i);
printf("%s + %s = %s\n", num1, num2, res);
if(i < n){
printf("\n");
}
}

return 0;
}

# Be Careful

  • 注意输出结果: 每个 case 之前会隔一个空行。最后一个 case 后面没有空行。注意读题。

# 长点心吧

理清需求啊

# 链接

# 最后

希望与你一起遇见更好的自己

期望与你一起遇见更好的自己

更新于 阅读次数

请我喝[咖啡]~( ̄▽ ̄)~*

方小白 微信支付

微信支付

方小白 支付宝

支付宝

方小白 numberpay

numberpay