zoukankan      html  css  js  c++  java
  • 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
    ", Solve( n ));
        return 0;
    }
     
  • 相关阅读:
    [C#][控件]WebBrowser 使用范例
    [java]经验集
    [html][easyui]DataGrid 绑定
    [转]jQuery 读取 xml
    [转][html]大文件下载
    [转][javascript]判断传入参数
    [html][javascript] Cookie
    [bat]批处理删默认共享和清理垃圾
    [转]JavaScript RegExp 对象参考手册
    5个编程问题(1小时解决)
  • 原文地址:https://www.cnblogs.com/Pretty9/p/7466150.html
Copyright © 2011-2022 走看看