POJ 2577: Interpreter
简略解题报告
Description
- 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
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
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的更多相关文章
- poj 3225 Help with Intervals(线段树,区间更新)
Help with Intervals Time Limit: 6000MS Memory Limit: 131072K Total Submissions: 12474 Accepted: ...
- poj 3225 【线段树】
poj 3225 这题是用线段树解决区间问题,看了两天多,算是理解一点了. Description LogLoader, Inc. is a company specialized in provid ...
- Dreamweaver 扩展开发:C-level extensibility and the JavaScript interpreter
The C code in your library must interact with the Dreamweaver JavaScript interpreter at the followin ...
- OpenCASCADE Expression Interpreter by Flex & Bison
OpenCASCADE Expression Interpreter by Flex & Bison eryar@163.com Abstract. OpenCASCADE provide d ...
- 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 ...
- POJ 3370. Halloween treats 抽屉原理 / 鸽巢原理
Halloween treats Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7644 Accepted: 2798 ...
- POJ 2356. Find a multiple 抽屉原理 / 鸽巢原理
Find a multiple Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7192 Accepted: 3138 ...
- POJ 2965. The Pilots Brothers' refrigerator 枚举or爆搜or分治
The Pilots Brothers' refrigerator Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 22286 ...
- POJ 1753. Flip Game 枚举or爆搜+位压缩,或者高斯消元法
Flip Game Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37427 Accepted: 16288 Descr ...
随机推荐
- Python自学笔记_
1. if语句 判断语句. 1 a=2 2 b=3 3 if a>b: 4 print("a>b") 5 else: 6 print("a<b" ...
- Eclipse使用Mybatis-Generator插件
Mybatis-Generator插件极大地方便了我们的开发效率,不用每张表每个字段人工去敲,所以本文介绍使用Mybatis-Generator自动生成Dao.Model.Mapping相关文件 版权 ...
- 使用kickstart + pxe 部署无人值守安装
1.作为中小公司的运维,经常会遇到一些机械式的重复工作,例如:有时公司同时上线几十甚至上百台服务器,而且需要我们在短时间内完成系统安装. 常规的安装系统方法: 光盘安装系统:一个服务器DVD内置光驱百 ...
- constraint the design
- GTX 750等低配显卡如何玩转Deepfakes?
这里说的Deepfakes软件还是DeepFaceLab,人工智能换脸,是使用深度学习方法来实现的.而深度学习程序对电脑配置要求是非常高的,尤其是跑模型这个环节.很多低配电脑,根本就跑步起来.比如像G ...
- PHP做ERP, CRM, CMS系统需要注意哪些地方
php作为二次开发弱类型语言, 可读性, 可视度都是比较高的. 在很多人眼里, 也许php只能做一些web应用开发, 比如某个公司的网站, 某个公司的网站后台, 其实,我可以告诉大家, php不比任何 ...
- Buffered Channels and Worker Pools
原文链接:https://golangbot.com/buffered-channels-worker-pools/ buffered channels 带有缓冲区的channel 只有在缓冲区满之后 ...
- Python9-进程-day36
import osfrom multiprocessing import Processimport timedef func(args,args2): print(args,args2) time. ...
- python寻找模块的路径顺序
>>> import sys >>> sys.path ['', '/Library/Frameworks/Python.framework/Versions/3. ...
- ASCII码表含义
在计算机中,所有的数据在存储和运算时都要使用二进制数表示(因为计算机用高电平和低电平分别表示1和0),例如,像a.b.c.d这样的52个字母(包括大写)以及0.1等数字还有一些常用的符号(例如*.#. ...