欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ZOJ1881 Interpreter【模拟】

程序员文章站 2022-07-15 16:35:05
...

Interpreter

Time Limit: 2 Seconds      Memory Limit: 65536 KB

A certain computer has 10 registers and 1000 words of RAM. Each register or RAM location holds a 3-digit integer between 0 and 999. Instructions are encoded as 3-digit integers and stored in RAM. The encodings are as follows:

  • 100 means halt
  • 2dn means set register d to n (between 0 and 9)
  • 3dn means add n to register d
  • 4dn means multiply register d by n
  • 5ds means set register d to the value of register s
  • 6ds means add the value of register s to register d
  • 7ds means multiply register d by the value of register s
  • 8da means set register d to the value in RAM whose address is in register a
  • 9sa means set the value in RAM whose address is in register a to the value of register s
  • 0ds means goto the location in register d unless register s contains 0

All registers initially contain 000. The initial content of the RAM is read from standard input. The first instruction to be executed is at RAM address 0. All results are reduced modulo 1000.


Input

The input to your program consists of up to 1000 3-digit unsigned integers, representing the contents of consecutive RAM locations starting at 0. Unspecified RAM locations are initialized to 000.

Input contains multiple test cases. Subsequent test cases are separated with a single blank line.


Output

The output for each test case from your program is a single integer: the number of instructions executed up to and including the halt instruction. You may assume that the program does halt.


Sample Input


299
492
495
399
492
495
399
283
279
689
078
100
000
000
000


Sample Output

16


Source: University of Waterloo Local Contest 2000.09.30


问题链接ZOJ1881 Interpreter

问题简述:(略)

问题分析

  这是一个模拟题,模拟指令的执行过程,内容包括指令、寄存器和存储器。

程序说明

  需要注意输入格式的处理。


题记:(略)


参考链接:(略)


AC的C++语言程序如下:

/* ZOJ1881 Interpreter */

#include <iostream>
#include <string.h>

using namespace std;

const int N1 = 1000;
const int N2 = 10;
int ram[N1], reg[N2];

int main()
{
    for(;;) {
        memset(reg, 0, sizeof reg);
        memset(ram, 0, sizeof ram);

        int add = 0, ans;
        string inst;
        while(getline(cin, inst)) {
            if(inst.empty())
                break;

            for(int i=0; inst[i]; i++)
                ram[add] = ram[add] * 10 + inst[i] - '0';

            add++;
        }

        if(add == 0)
            break;

        ans = add = 0;
        for(;;) {
            ans++;
            int cmd = ram[add++];
            if(cmd == 100)
                break;

            int op = cmd / 100, operand1 = cmd % 100 / 10, operand2 = cmd % 10;
            switch(op) {
            case 0:
                if(reg[operand2]) add = reg[operand1];
                break;
            case 2:
                reg[operand1] = operand2;
                break;
            case 3:
                (reg[operand1] += operand2) %= 1000;
                break;
            case 4:
                (reg[operand1] *= operand2) %= 1000;
                break;
            case 5:
                reg[operand1] = reg[operand2];
                break;
            case 6:
                (reg[operand1] += reg[operand2]) %= 1000;
                break;
            case 7:
                (reg[operand1] *= reg[operand2]) %= 1000;
                break;
            case 8:
                reg[operand1] = ram[reg[operand2]];
                break;
            case 9:
                ram[reg[operand2]] = reg[operand1];
                break;
            }
        }

        cout << ans << '\n';
    }

    return 0;
}