-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path13.cpp
More file actions
93 lines (78 loc) · 1.53 KB
/
Copy path13.cpp
File metadata and controls
93 lines (78 loc) · 1.53 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*
Work out the first ten digits of the sum of the following one-hundred 50-digit numbers.
*/
#include <cstdlib>
#include <iomanip>
#include <iostream>
#include <stack>
#include <string>
using namespace std;
const int DIGITS = 50;
const int N = 100;
int **matrix;
void allocateMatrix() {
matrix = new int *[N];
for (int i = 0; i < N; ++i) {
matrix[i] = new int[DIGITS];
}
}
void freeMatrix() {
for (int i = 0; i < N; ++i) {
delete[] matrix[i];
}
delete[] matrix;
}
void printMatrix() {
for (int i = 0; i < N; i++) {
for (int j = 0; j < DIGITS; j++) {
cout << matrix[i][j];
}
cout << endl;
}
}
void calculate() {
int carry = 0;
int sum;
stack<int> stack;
for (int i = DIGITS - 1; i >= 0; i--) {
sum = 0;
for (int j = 0; j < N; j++) {
sum += matrix[j][i];
}
sum += carry;
if (i == 0) {
stack.push(sum);
} else {
stack.push(sum % 10);
carry = sum / 10;
cout << carry << endl;
}
}
for (int i = 0; i < DIGITS; i++) {
cout << stack.top();
stack.pop();
}
}
int main() {
string a = "123";
allocateMatrix();
int testSum1 = 0;
int testSum2 = 0;
for (int i = 0; i < N; i++) {
cin >> a;
for (int j = 0; j < DIGITS; j++) {
matrix[i][j] = a[j] - '0';
if (j == DIGITS - 1) {
testSum1 += matrix[i][j];
} else if (j == DIGITS - 2) {
testSum2 += matrix[i][j];
}
}
}
cout << testSum1 << endl;
cout << testSum2 << endl;
// printMatrix();
calculate();
freeMatrix();
return 0;
}