UVA 10033 - Interpreter

Description:
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 begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.
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.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.
The output 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

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

Sample Output

16

题意:给出一系列的指令,求总共执行的指令数;

InstructionsMeaning
100停止执行
2dn设置寄存器d的值为n(0-9)
3dn寄存器d的值加上n
4dn寄存器d的值乘上n
5ds设置寄存器d的值为寄存器s的值
6ds寄存器d的值加上寄存器s的值
7ds寄存器d的值乘上寄存器s的值
8da设置寄存器d的值为RAM中地址为寄存器a的值
9sa设置RAM中地址为寄存器a的值为寄存器s的值
0ds跳转到RAM中地址为寄存器d的值处,除非此时寄存器s的值为0

思路:按照所给的指令进行模拟即可,要注意的是在输入第一个测试数的时候后面要跟一个空行,并且每两个测试用例之间要有一个空行;再有一个需要注意的是,虽然题目告诉我们RAM中的指令数只有1000条,但是不代表输入的指令数不超过1000条,所以数组RAM开的大一点,否则会出现Runtime Error;最后一点,在计算总共执行的指令数时,要加上停止指令的执行;

#include <cstdio>
#include <cstring>
#include <memory>

int RAM[10000];
int reg[10+5];

#define op1(num) (num/100)
#define op2(num) (num/10%10)
#define op3(num) (num%10)

int execute(){
    int cur = 0;   //the position of RAM
    int cnt = 0+1; //the number of instructions executed
    int op;        //current instruction

    while(1){
        op = RAM[cur++];
        switch(op1(op)){
        case 0:
            if(reg[op3(op)])
                cur = reg[op2(op)];
            break;
        case 1:
            if(!op2(op) && !op3(op))
                return cnt;
            break;
        case 2:
            reg[op2(op)] = op3(op);
            break;
        case 3:
            reg[op2(op)] = (reg[op2(op)] + op3(op))%1000;
            break;
        case 4:
            reg[op2(op)] = (reg[op2(op)] * op3(op))%1000;
            break;
        case 5:
            reg[op2(op)] = reg[op3(op)];
            break;
        case 6:
            reg[op2(op)] = (reg[op2(op)] + reg[op3(op)])%1000;
            break;
        case 7:
            reg[op2(op)] = (reg[op2(op)] * reg[op3(op)])%1000;
            break;
        case 8:
            reg[op2(op)] = RAM[reg[op3(op)]];
            break;
        case 9:
            RAM[reg[op3(op)]] = reg[op2(op)];
            break;
        default:
            break;
        }
        cnt++;
    }
}

int main(){
    int N;
    scanf("%d\n\n", &N);

    while(N--){
        char buff[10];
        int cnt = 0;
        memset(RAM, 0, sizeof(RAM));
        memset(reg, 0, sizeof(reg));
        while(gets(buff)){
            if(!strcmp(buff, ""))
                break;
            sscanf(buff, "%d", RAM+cnt);
            cnt++;
        }
        printf("%d\n", execute());
        if(N)
            printf("\n");
    }

    return 0;
}

python023基于Python旅游景点推荐系统带vue前后端分离毕业源码案例设计 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值