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;
}
 
时间: 2024-08-05 11:00:19

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

POJ 模拟题集合

http://www.cppblog.com/Uriel/articles/101592.html 感觉这个暑假没有去年有激情啊,,,还没到状态就已经块上学了,,, 真是弱暴了,,,找几道模拟题刷刷... 标加号表示已AC... + 1008   历法,不难 + 1102   不难. + 1028   纯模拟.被题目坑了一下.. 1023   貌似搞了一会儿.. 1051   算是模拟,写得比较麻烦,要细心 1099   跟化学式有关的模拟,有意思,高兴的是这题完全是自己想的AC的.. 1107

python-GIL(Global Interpreter Lock)

1. 不管计算机是几核的,在Python中都是串行的.只不过是在不停地进行上下文的切换. 转发自下面网址: http://python.jobbole.com/81822/ 前言:博主在刚接触Python的时候时常听到GIL这个词,并且发现这个词经常和Python无法高效的实现多线程划上等号.本着不光要知其然,还要知其所以然的研究态度,博主搜集了各方面的资料,花了一周内几个小时的闲暇时间深入理解了下GIL,并归纳成此文,也希望读者能通过次本文更好且客观的理解GIL. GIL是什么 首先需要明确的

C++编程实现对工厂产品生产流程的模拟

花费二个多月的时间编写了可以实时模拟工厂产品生产流程的程序,工厂产品生产流程的模拟,就是计算在工艺文件所规定的工序下,不同种类的多件产品(同一类别的产品可以有多件)在不同类别的多台设备(同一类别的设备可以有多台)上全部生产完毕所需的总时间.每一件产品可以在生产流程中先后多次在同一类设备上生产而且生产时间可以不同,某一给定时刻多件产品有可能需要在同一台设备上生产,造成设备占用冲突,这时必须按一定的算法对造成冲突的产品在设备等待队列上按优先级排队.产品在设备上加工完毕准备进入下一台设备生产时有同类型

CentOS系统启动及内核大破坏模拟实验

讲过了centos的启动流程,此时是不是想来点破坏呢?那就尽情的玩耍吧,记得在实验之前拍个快照,万一哪个环节错误恢复不回来了呢,毕竟数据无价,话不多说,开始. 一.删除伪系统根.(ramdisk文件) (1)模拟误操作删除ramdisk文件. ①模拟误删除initramfs-3.10.0-514.el7.x86_64.img文件. ②为当前正在使用的内核重新制作ramdisk文件 格式为:mkinitrd /boot/initramfs-$(uname -r).img $(uname -r) (

NYOJ 2356: 哈希计划【模拟】

题目描述 众所周知,LLM的算法之所以菜,就是因为成天打游戏,最近LLM突然想玩<金庸群侠传X>,结果进去后各种被虐,LLM就开始研究这个游戏的代码,顺便还学会了一点点点点lua语言,然后就开始了伟大的改游戏代码之旅,然后LLM发现自己too young了,这个游戏把所有的文本都进行了哈希,如果自己改了代码或者剧情文本的话它哈希出来的值就会和原来的哈希值不一样......然后游戏就会打不开.....,现在LLM发现了文本的哈希函数,要求你写个程序,功能为: 输入一段字符串,输出一个哈希值 为了

爬虫——模拟点击动态页面

动态页面的模拟点击: 以斗鱼直播为例:http://www.douyu.com/directory/all 爬取每页的房间名.直播类型.主播名称.在线人数等数据,然后模拟点击下一页,继续爬取 #!/usr/bin/python3 # -*- conding:utf-8 -*- __author__ = 'mayi' """ 动态页面的模拟点击: 模拟点击斗鱼直播:http://www.douyu.com/directory/all 爬取每页房间名.直播类型.主播名称.在线人数

爬虫——网站模拟登录

使用Selenium与PhantomJS模拟登录豆瓣:https://www.douban.com/ #!/usr/bin/python3 # -*- conding:utf-8 -*- __author__ = 'mayi' """ 模拟登录豆瓣:https://www.douban.com/ """ from selenium import webdriver # 调用环境变量指定的PhantomJS浏览器创建浏览器对象,executable

python爬虫 模拟登陆校园网-初级

最近跟同学学习爬虫的时候看到网上有个帖子,好像是山大校园网不稳定,用py做了个模拟登陆很有趣,于是我走上了一条不归路..... 先上一张校园网截图 首先弄清一下模拟登陆的原理: 1:服务器判定浏览器登录使用浏览器标识,需要模拟登陆 2: 需要post账号,密码,以及学校id python走起,我用的2.7版本,用notepad++写的,绑定python可以直接运行 由于是模拟网页登陆,需要导入urllib urllib2 cookielib库,前两个有与网页直接的接口,cookielib就是用来

Android模拟位置信息

Android模拟位置程序,俗称GPS欺骗,只能修改采用GPS定位的软件. 手机定位方式目前有4种:基站定位,WIFI定位,GPS定位,AGPS定位 常见的修改手法: 1. 抓包欺骗法,抓包改包欺骗服务器端, 但是得专门去针对某款app,而且现在很多app数据包都加密了 2. hook java层经纬度获取函数, 这个方法以前可以用,现在不行了 3. hook native层经纬度获取函数 4. 使用允许模拟地址位置信息(不是很通用有版本限制) 为了修改微信朋友圈地理位置信息,为了好玩 试过了上