HDUST-1245 Interpreter(模拟)

1245: Problem E: Interpreter

时间限制: 1 Sec  内存限制: 128 MB
提交: 4  解决: 2
[提交][状态][讨论版]

题目描述

 

Problem E: Interpreter

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.

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.

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.

输入

 

输出

 

样例输入

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

样例输出

16
#include<bits/stdc++.h>

using namespace std;
const int N = 1000 + 5;
const int M = 10 + 5;
#define mod %1000

int a[N], reg[M];

int Solve(int n){
    auto cnt = 0;
    for(int i = 0; ; i++){
        ++ cnt;
        int t = a[i] / 100, x = (a[i] % 100) / 10, y = a[i] % 10;
        a[i] = a[i] mod;
        if(!t && reg[y]) i = reg[x] - 1;
        else if(t == 1) return cnt;
        else if(t == 2) reg[x] = y;
        else if(t == 3) reg[x] = (reg[x] + y) mod;
        else if(t == 4) reg[x] = (reg[x] * y) mod;
        else if(t == 5) reg[x] = reg[y];
        else if(t == 6) reg[x] = (reg[x] + reg[y]) mod;
        else if(t == 7) reg[x] = (reg[x] * reg[y]) mod;
        else if(t == 8) reg[x] = a[reg[y]] mod;
        else if(t == 9) a[reg[y]] = reg[x] mod;
    }
    return -1;
}
int main(){
    char ch[5];
    int n = 0;
    while(fgets(ch, 5, stdin) != NULL){
        sscanf(ch, "%d", &a[n ++]);
    }
    printf("%d\n", Solve( n ));
    return 0;
}

 

 

转载于:https://www.cnblogs.com/Pretty9/p/7466150.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值