简略解题报告

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. 解决linux不能解压rar格式压缩包

    1download rarlinux-x64-5.3.0.tar.gz data package 2.tar xvf rarlinux-64-5.3.0.tar.gz 3. cd rar and th ...

  2. 【markdown】 markdown 语法

    介绍几个 markdown 语法学习地址和相关工具 参考链接 coding gitlab markdown offical markdown editor markdown editor2

  3. python操作日志的封装

    前言 曾经转载过一篇关于python日志模块logging的详解 https://www.cnblogs.com/linuxchao/p/linuxchao-log.html, 虽然这篇文章是别人写的 ...

  4. day13-生成器

    def generator(): print(1) yield 'a' rcp = generator() print(rcp.__next__()) 只要含有yield关键字的函数都是生成器函数.y ...

  5. 安装好的IIS,发布成功后打开网站出现错误

      开发web项目时需要安装IIS,在安装好IIS的Windows7本上发布asp.net网站时,web程序已经映射到了本地IIS上,但运行如下错误提示“处理程序“PageHandlerFactory ...

  6. SSAS——MDX基础

    一.基本概念 MDX:一种查询语言,从多维的数据集单元格中检索数据.支持两种不同的模式: 1.表达式语言:定义和操纵Analysis Services对象和数据以计算值 2.查询语言:从Analysi ...

  7. PHP GD库---之头像合成九宫格

    public function createMosaicGroupAvatar($pic_list = array(), $bg_w = 396, $bg_h = 396) { if (!$pic_l ...

  8. UVa 1366 DP Martian Mining

    网上的题解几乎都是一样的: d(i, j, 0)表示前i行前j列,第(i, j)个格子向左运输能得到的最大值. d(i, j, 1)是第(i, j)个格子向上运输能得到的最大值. 但是有一个很关键的问 ...

  9. UVa 10110 Light, more light

    开始所有的灯是灭的,不过我们只关心最后一个灯. 在第i次走动时,只有编号为i的倍数的灯的状态才会改变. 也就是说n有偶数个约数的时候,最后一个灯的状态不会改变,也就是灭的. n有奇数个约数的时候也就是 ...

  10. MIME类型-服务端验证上传文件的类型

    MIME的作用 : 使客户端软件,区分不同种类的数据,例如web浏览器就是通过MIME类型来判断文件是GIF图片,还是可打印的PostScript文件. web服务器使用MIME来说明发送数据的种类, ...