-
Notifications
You must be signed in to change notification settings - Fork 13
/
mayan-calculation.c
108 lines (81 loc) · 1.92 KB
/
mayan-calculation.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
void readAlphabet(char M[20][2000], int L, int H)
{
for (int y = 0; y < H; y++)
{
char numeral[2048];
scanf("%s", numeral);
fgetc(stdin);
for (int i = 0; i < 20; i++)
for (int x = 0; x < L; x++) M[i][y * L + x] = numeral[i * L + x];
}
}
long long int readNum(char M[20][2000], int L, int H)
{
char tmp[2000];
long long int R = 0;
int S;
scanf("%d", &S);
fgetc(stdin);
long long int b = pow(20, S / H - 1);
for (int i = 0; i < S;) {
for (int y = 0; y < H; y++) { scanf("%s", (tmp + y * L)); fgetc(stdin); }
for (int j = 0; j < 20; j++)
if (0 == memcmp(M[j], tmp, H * L)) { R+= b * j; break; }
b/= 20;
i+= H;
}
return R;
}
void printNum(char M[20][2000], int L, int H, long long int n) {
long long int nums[16] = {0}; // log20(2^64)
int i = 0;
if (n == 0)
i++;
while (n) {
long long int t = n / 20;
long long int r = n - t * 20;
nums[i++] = r;
n = t;
}
while (i > 0) {
n = nums[--i];
for (int j = 0; j < L * H; j++) {
printf("%c", M[n][j]);
if ((j % L) == (L - 1)) {
printf("\n");
}
}
}
}
int main()
{
int L, H;
char M[20][2000];
char OP;
long long int N1, N2;
scanf("%d%d", &L, &H);
fgetc(stdin);
readAlphabet(M, L, H);
N1 = readNum(M, L, H);
N2 = readNum(M, L, H);
scanf("%c", &OP);
fgetc(stdin);
switch (OP) {
case '+':
printNum(M, L, H, N1 + N2);
break;
case '*':
printNum(M, L, H, N1 * N2);
break;
case '-':
printNum(M, L, H, N1 - N2);
break;
case '/':
printNum(M, L, H, N1 / N2);
break;
}
return 0;
}