Skip to content
Permalink
master
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
#include <iostream>
#include <cstring>
#include <sstream>
using namespace std;
int ram[1000], registers[10];
int noExec (int (&ram)[1000])
{
int no, a, b, c, nrInstr = 1;
for (no = 0; ram[no] != 100;)
{
++nrInstr;
c = ram[no] / 100;
a = (ram[no] / 10) % 10;
b = ram[no] % 10;
no++;
switch (c)
{
case 2:
registers[a] = b;
break;
case 3:
registers[a] = (registers[a] + b) % 1000;
break;
case 4:
registers[a] = (registers[a] * b) % 1000;
break;
case 5:
registers[a] = registers[b];
break;
case 6:
registers[a] = (registers[a] + registers[b]) % 1000;
break;
case 7:
registers[a] = (registers[a] * registers[b]) % 1000;
break;
case 8:
registers[a] = ram[ registers[b] ];
break;
case 9:
ram[ registers[b] ] = registers[a];
break;
case 0:
if (registers[b]) no = registers[a];
break;
}
}
return nrInstr;
}
int main()
{
int noCases;
string line;
cin >> noCases;
getline (cin, line); // go to the second line
getline (cin, line);; // skip the blank second line
for (; noCases; --noCases)
{
memset (ram, 0, sizeof (ram));
memset (registers, 0, sizeof (registers));
int noInstr (0);
while (getline (cin, line))
{
if (line.empty()) break;
istringstream iss (line);
iss >> ram[noInstr++];
}
cout << noExec (ram) << endl;
if (noCases > 1) cout << endl;
}
return 0;
}