-
Notifications
You must be signed in to change notification settings - Fork 2
/
00394.cpp
75 lines (64 loc) · 1.58 KB
/
00394.cpp
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
#include<iostream>
#include<map>
using namespace std;
//Using long longs just to be safe
typedef long long ll;
struct array
{
string name;
ll base;
ll byte;
ll dim;
ll upper[11];
ll lower[11];
ll constant[11];
};
int main()
{
ll n,r;
ll indexes[11];
map<string, ll> order;
cin >> n >> r;
array* arrays = new array[n];
//Build each array
for(ll i = 0; i < n; i++)
{
//Get name, base address, byte size, and number of dimensions
cin >> arrays[i].name >> arrays[i].base >> arrays[i].byte >> arrays[i].dim;
//Keep track of the order the arrays came in
order[arrays[i].name] = i;
arrays[i].constant[ arrays[i].dim ] = arrays[i].byte;
//Get the upper and lower bounds
for(ll j = 1; j <= arrays[i].dim; j++)
cin >> arrays[i].lower[j] >> arrays[i].upper[j];
//Build the constant array starting fromt the highest dimension
for(ll j = arrays[i].dim -1; j >= 1; j--)
{
arrays[i].constant[j] = arrays[i].constant[j+1] * (arrays[i].upper[j+1] - arrays[i].lower[j+1] + 1);
}
}
for(ll i = 0; i < r; i++)
{
string name;
ll address = 0;
cin >> name;
cout << name << '[';
ll arrNum = order[name];
//Get the indexes
for(ll j = 1; j <= arrays[arrNum].dim; j++)
{
cin >> indexes[j];
if(j != 1)
cout << ", ";
cout << indexes[j];
}
cout << "] = ";
//Calculaate the address
address += arrays[arrNum].base;
for(ll i = 1; i <= arrays[arrNum].dim; i++)
address += arrays[arrNum].constant[i]*(indexes[i]- arrays[arrNum].lower[i]);
cout << address << endl;
}
delete[] arrays;
return 0;
}