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 = + ;
const int M = + ;
#define mod %1000 int a[N], reg[M]; int Solve(int n){
auto cnt = ;
for(int i = ; ; i++){
++ cnt;
int t = a[i] / , x = (a[i] % ) / , y = a[i] % ;
a[i] = a[i] mod;
if(!t && reg[y]) i = reg[x] - ;
else if(t == ) return cnt;
else if(t == ) reg[x] = y;
else if(t == ) reg[x] = (reg[x] + y) mod;
else if(t == ) reg[x] = (reg[x] * y) mod;
else if(t == ) reg[x] = reg[y];
else if(t == ) reg[x] = (reg[x] + reg[y]) mod;
else if(t == ) reg[x] = (reg[x] * reg[y]) mod;
else if(t == ) reg[x] = a[reg[y]] mod;
else if(t == ) a[reg[y]] = reg[x] mod;
}
return -;
}
int main(){
char ch[];
int n = ;
while(fgets(ch, , stdin) != NULL){
sscanf(ch, "%d", &a[n ++]);
}
printf("%d\n", Solve( n ));
return ;
}
 

HDUST-1245 Interpreter(模拟)的更多相关文章

  1. PL/0编译器(java version) - Interpreter.java

    1: package compiler; 2:   3: import java.io.BufferedReader; 4: import java.io.BufferedWriter; 5: imp ...

  2. POJ 模拟题集合

    http://www.cppblog.com/Uriel/articles/101592.html 感觉这个暑假没有去年有激情啊,,,还没到状态就已经块上学了,,, 真是弱暴了,,,找几道模拟题刷刷. ...

  3. python之GIL官方文档 global interpreter lock 全局解释器锁

    0.目录 2. 术语 global interpreter lock 全局解释器锁3. C-API 还有更多没有仔细看4. 定期切换线程5. wiki.python6. python.doc FAQ ...

  4. python模拟面试技术题答案

      目录 Python4期模拟面试技术面试题答案............................................................................ ...

  5. Java 模拟面试题

    1.面向对象的特点 继承,封装,多态 2.对象和类的区别是什么? 对象是对客观事物的抽象,类是对对象的抽象.类是一种抽象的数据类型,它们的关系是,对象是类的实例,类是对象的模板. 3.静态成员和实例成 ...

  6. javascript设计模式 - 解释器模式(interpreter)

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Python Interpreter

    在开始之前,我们先限定下python解释器的意思.当讨论Python的时候,解释器这个词可以用在不同的地方.有的时候,解释器指的是Python Interpreter,也就是你在命令行交互界面上输入p ...

  8. 解释器模式Interpreter详解

    原文链接:https://www.cnblogs.com/java-my-life/archive/2012/06/19/2552617.html 在阎宏博士的<JAVA与模式>一书中开头 ...

  9. 如何实现模拟器(CHIP-8 interpreter) 绝佳杰作.

    转自 http://www.multigesture.net/articles/how-to-write-an-emulator-chip-8-interpreter/ How to write an ...

随机推荐

  1. ABP .net Core MQTT+signalr通讯

    abp版本: 4.3.0.0 .net core 版本 2.2 1.Mqtt 1.1 添加程序集:M2MqttDotnetCore(差点以为没有.net core 的) 2.2 实现代码:抄了个单例模 ...

  2. Java indexOf() 方法

    indexOf() 方法有以下四种形式: public int indexOf(int ch): 返回指定字符在字符串中第一次出现处的索引,如果此字符串中没有这样的字符,则返回 -1. public ...

  3. Unity3D_(API)场景切换SceneManager

    Unity场景切换SceneManager 官方文档:传送门 静态方法 创建场景 CreateScene Create an empty new Scene at runtime with the g ...

  4. docker删除容器再删除镜像

    1. 查询镜像 docker images 现在想删除第一个,ID为 99f85991949f 的镜像. docker rmi  ID 从上面看出,需要先停到ID为 67*** 的容器. 2. 查询容 ...

  5. Java_IO流实验

    实验题目链接:Java第09次实验(IO流) 0. 字节流与二进制文件 我的代码 package experiment.io; import java.io.DataInputStream; impo ...

  6. Ruby的异常处理

    Ruby在处理0.1+0.2是会出现精度问题: 许多语言都有类似问题,详见网址:http://0.30000000000000004.com/ Ruby的异常处理 如果异常处理范围是整个方法体,可以省 ...

  7. flutter Waiting for another flutter command to release the startup lock…

    flutter安装完成后执行flutter doctor ,一直提示如下: Waiting for another flutter command to release the startup loc ...

  8. rocketMQ 订阅关系

    场景:2 个消费者进程中,创建了 2 个消费者,同属于 1 个消费组,但是订阅了不同的 topic,会因为订阅信息相互覆盖,导致拉不到消息. 原因是 rocketMQ 的订阅关系,是根据 group ...

  9. python unittest中setUp()和setUpClass()、tearDown()和tearDownClass()之间的区别

    setUp():每个测试case运行之前运行tearDown():每个测试case运行完之后执行setUpClass():必须使用@classmethod 装饰器,  所有case运行之前只运行一次t ...

  10. Java学习之==>注释、数据类型、变量、运算符

    一.注释 在Java中有3种标记注释的方式,最常用的方式是 // ,其注释的内容从 // 开始,到本行结束.但需要注意的是,我们不建议把注释写在代码的尾部(即尾注释),我们建议把注释写在代码的上一行, ...