-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHDU_1002.cpp
More file actions
70 lines (70 loc) · 1.3 KB
/
HDU_1002.cpp
File metadata and controls
70 lines (70 loc) · 1.3 KB
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
#include <iostream>
#define N 1001
using namespace std;
int Get_Length(const char *str);
void Reverse_String(char s[],int n);
int main()
{
int loop;
int result;
while (cin >> loop)
{
for (int i=1;i<=loop;i++)
{
char A[N],B[N];
char C[1003];
cin >> A >> B;
cout << "Case " << i <<":"<<endl;
cout << A << " + " << B << " = ";
int A_length = Get_Length(A);
int B_length = Get_Length(B);
int length = A_length>B_length?A_length:B_length;
Reverse_String(A,A_length);
Reverse_String(B,B_length);
int remaind_digit = 0;
int carry_digit = 0;
for (int i=0;i<length;i++)
{
if (length>A_length&&i>=A_length)
{
result = B[i]-'0'+carry_digit;
}
else if (length>B_length&&i>=B_length)
{
result = A[i]-'0'+carry_digit;
}
else
result = A[i]+B[i]-'0'-'0'+carry_digit;
C[i] = result % 10 + '0';
carry_digit = result / 10;
}
if (carry_digit != 0)
{
C[length++] = carry_digit + '0';
}
Reverse_String(C,length);
C[length] = '\0';
cout << C <<endl;
}
}
return 0;
}
int Get_Length(const char *str)
{
int length = 0;
while (str[length] != '\0')
{
length++;
}
return length;
}
void Reverse_String(char s[],int n)
{
int temp;
for (int i=0;i<(n+1)/2;i++)
{
temp = s[i];
s[i] = s[n-1-i];
s[n-1-i] = temp;
}
}