ZeroJudge A349: Decoding Commands

Zhihau’s family owns a very precious antique computer. This computer has only four 16-bit registers and eight words (each word is also 16-bit) of main memory. The instruction format of its CPU is as follows:

CommandsCommand FormatsCommand Explanations
LOADLOAD RD MSLoad the word value from memory address MS into register RD.
STORESTORE MD RSStore the value of register RS back into the word at memory address MD.
ADDADD RD RS1 RS2Store the sum of the values in registers RS1 and RS2 into register RD.
MOVEMOVE RD RSStore the value in register RS into register RD.

Assuming that the register numbers start from 0, and the main memory addresses also start from 0, if the values at memory addresses 0 and 1 are known to be 123 and 456, the following code snippet will store 579 (123 + 456) into the word at memory address 2 after execution.

LOAD 0 0
LOAD 1 1
ADD 2 0 1
STORE 2 2

Please write a program that reads a series of instructions (only the four mentioned types will appear), simulates the execution of these instructions, and displays the contents of the first register and the first memory word after execution.

Assumption 1: The initial value of all registers is 0.

Assumption 2: There will be no overflow during the calculation process.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
The first eight lines represent the initial values of the eight memory words.
The ninth line contains a positive integer N (1 ≤ N ≤ 50), which represents the number of instructions.
Starting from the tenth line, there are N lines, each containing one instruction.
Please output the values of the first register (register number 0) and the first memory word (memory address 0).
123
456
0
0
0
0
0
0
4
LOAD 0 0
LOAD 1 1
ADD 2 0 1
STORE 2 2
123
123
100
200
300
400
0
0
0
0
7
LOAD 0 0
LOAD 1 1
ADD 2 0 1
LOAD 0 2
MOVE 3 2
ADD 0 3 1
STORE 3 0
500
100
ZeroJudge A349 Sample Test Cases

Thought Process

Declare two empty arrays and use conditional statements to collect data.

Sample Code-ZeroJudge A349: Decoding Commands

#include <iostream>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int num[8] = {}, tempMem[8] = {};
    for (int i = 0; i < 8; i++) {
        cin >> num[i];
        tempMem[i] = -1;
    }
    int N;
    cin >> N;
    for (int i = 0; i<N; i++) {
        string str;
        cin >> str;
        if (str == "LOAD") {
            int RD, MS;
            cin >> RD >> MS;
            tempMem[RD] = num[MS];
        }
        else if (str == "STORE") {
            int MD, RS;
            cin >> MD >> RS;
            num[MD] = tempMem[RS];
        }
        else if (str == "ADD") {
            int RD, RS1, RS2;
            cin >> RD >> RS1 >> RS2;
            tempMem[RD] = tempMem[RS1] + tempMem[RS2];
        }
        else if (str == "MOVE") {
            int RD, RS;
            cin >> RD >> RS;
            tempMem[RD] = tempMem[RS];
        }
    }
    cout << tempMem[0] << "\n" << num[0] << "\n";
}

//ZeroJudge A349
//Dr. SeanXD

Comments