简略解题报告

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 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

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

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

Sample Output

16
//POJ 2577
//题意:按题意实现一个解释器。模拟以前的某种计算机的CPU吧
//题型:简单模拟题
//思路:有条理就行
#include <cstdio>
#include <cstring> int hotal[];
int memory[];
int runedCommandNum; //读入本行数字,储存在对应内存位置
//若为空行,返回false
bool readIntInThisLine(int nowPosition) {
char now;
int num = ; now = getchar();
if (now == '\n') return false;
if (now == EOF) return false; while (now != '\n') {
num = num* + now-'';
now = getchar();
}
memory[nowPosition] = num%;
//printf("read %d (positon:%d[%d])\n", num%1000, nowPosition, memory[nowPosition]);
return true;
} //执行命令
//描述:从指定内存处执行命令,并通过参数返回下一条命令所在内存。
// 如果停机,返回false
bool runCommandAt(int nowPosition, int &nextPosition) {
char command[];
sprintf(command, "%03d", memory[nowPosition]);
//printf("command = %s\n", command); nextPosition = nowPosition+;
switch (command[]) {
case '':
//如果后面不是00,那是什么命令
if (command[] == '' && command[] == '') return false;
else return true;
case '':
hotal[command[]-''] = command[]-'';
hotal[command[]-''] %= ;
return true;
case '':
hotal[command[]-''] += command[]-'';
hotal[command[]-''] %= ;
return true;
case '':
hotal[command[]-''] *= command[]-'';
hotal[command[]-''] %= ;
return true;
case '':
hotal[command[]-''] = hotal[command[]-''];
hotal[command[]-''] %= ;
return true;
case '':
hotal[command[]-''] += hotal[command[]-''];
hotal[command[]-''] %= ;
return true;
case '':
hotal[command[]-''] *= hotal[command[]-''];
hotal[command[]-''] %= ;
return true;
case '':
hotal[command[]-''] = memory[hotal[command[]-'']];
hotal[command[]-''] %= ;
return true;
case '':
memory[hotal[command[]-'']] = hotal[command[]-''] ;
memory[hotal[command[]-'']] %= ;
return true;
case '':
if (hotal[command[]-''] != ) {
nextPosition = hotal[command[]-''];
}
return true;
}
} // 开机
// 描述:开机运行命令,停机后输出命令数
void run() {
int nowPosition = ;
int nextPosition; runedCommandNum = ;
while (runCommandAt(nowPosition, nextPosition)) {
//printf("nextPosition = %d, command = %03d\n", nextPosition, memory[nextPosition]);
nowPosition = nextPosition;
runedCommandNum++;
}
runedCommandNum++;
printf("%d\n", runedCommandNum);
} int main() {
//int t;
//scanf("%d", &t);
//scanf("%*[ \n]");
//while (t--) {
// memset(memory, 0, sizeof(memory));
// memset(hotal, 0, sizeof(hotal));
// int nowPosition = 0;
// while(readIntInThisLine(nowPosition) == true) nowPosition++;
// run();
//}
int n;
memset(memory, , sizeof(memory));
memset(hotal, , sizeof(hotal));
int now = ;
while (scanf("%d", &n) != EOF) {
memory[now++] = n%;
}
run();
return ;
}

POJ 2577: Interpreter的更多相关文章

  1. poj 3225 Help with Intervals(线段树,区间更新)

    Help with Intervals Time Limit: 6000MS   Memory Limit: 131072K Total Submissions: 12474   Accepted:  ...

  2. poj 3225 【线段树】

    poj 3225 这题是用线段树解决区间问题,看了两天多,算是理解一点了. Description LogLoader, Inc. is a company specialized in provid ...

  3. Dreamweaver 扩展开发:C-level extensibility and the JavaScript interpreter

    The C code in your library must interact with the Dreamweaver JavaScript interpreter at the followin ...

  4. OpenCASCADE Expression Interpreter by Flex & Bison

    OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...

  5. PhpStorm和WAMP配置调试参数,问题描述Error. Interpreter is not specified or invalid. Press “Fix” to edit your project configuration.

    PhpStorm和WAMP配置调试参数 问题描述: Error. Interpreter is not specified or invalid. Press “Fix” to edit your p ...

  6. POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理

    Halloween treats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7644   Accepted: 2798 ...

  7. POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理

    Find a multiple Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7192   Accepted: 3138   ...

  8. POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治

    The Pilots Brothers' refrigerator Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 22286 ...

  9. POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法

    Flip Game Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37427   Accepted: 16288 Descr ...

随机推荐

  1. MySQL8.0.12安装及配置

    一.下载 下载页面http://dev.mysql.com/downloads/mysql/ 选择系统平台后,点击download(根据系统选择64或32位) 二.配置 1.下载成功后,解压安装包到要 ...

  2. linux中怎样关闭ICMP回应功能

    引用自:http://blog.csdn.net/qq844352155/article/details/49700121 linux中怎样关闭ICMP回应功能   输入:   echo 1 > ...

  3. COMP9021--6.17

    1. ''' '''the comment in the middle will be shown in your code while ranning 2. a=bc=a%bor we can si ...

  4. (转)可简化iOS 应用程序开发的6个Xcode小技巧

    Xcode是iPhone和iPad开发者用来编码或者开发iOS app的IDE.Xcode有很多小巧但很有用的功能,很多时候我们可能没有注意到它们,也或者我们没有在合适的水平使用这些功能简化我们的iO ...

  5. CentOS 7 配置OpenCL环境(安装NVIDIA cuda sdk、Cmake、Eclipse CDT)

    序 最近需要在Linux下进行一个OpenCL开发的项目,现将开发环境的配置过程记录如下,方便查阅. 完整的环境配置需要以下几个部分: 安装一个OpenCL实现,基于硬件,选择NVIDIA CUDA ...

  6. golang连接orcale

    使用glang有一段时间了,最开始其实并不太喜欢他的语法,但是后来熟悉之后发现用起来还挺爽的.之前数据库一直使用mysql,连接起来没有什么问题,github上有很多完善的驱动,所以以为连接其他数据库 ...

  7. UVA10779Collectors Problem

    uva 10779 Collectors Problem Some candy manufacturers put stickers into candy bar packages. Bob and ...

  8. Python虚拟机之异常控制流(五)

    Python中的异常控制语义结构 在Python虚拟机之异常控制流(四)这一章中,我们考察了Python的异常在虚拟机中的级别上是什么东西,抛出异常这个动作在虚拟机的级别上对应的行为,最后,我们还剖析 ...

  9. PYday15--面向对象的进阶:集成、成员、方法、异常处理

    1.继承 实例: 2.构造方法: 3.反射:以字符串的形式去模块操作其成员. 成员: 最外层是文件,文件里面包含类,通过类可以创建对象,对象可以封装字段和指针.类里面可以有方法,指针可以指向方法. 通 ...

  10. python学习--学习时间属性的应用(time / datetime )

    #!/usr/bin/python # -*- coding:utf-8 -*- # import time # myd={1:'a',2:'b'}# for key,value in dict.it ...