简略解题报告

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. php 关于金额的几种计算方式

    php 关于金额的几种计算方式 平常开始开发过程中,多多少少都会遇到点关于金额的计算,比如设置返利.提现手续费.折扣啊等等诸如此类的比例,然后再计算出之后的实际的费用. 下面,以折扣为例,来实现这类计 ...

  2. 安装openstack同步数据库时出错解决方法

    错误提示:(2003, "Can't connect to MySQL server on 'controller' ([Errno -2] Name or service not know ...

  3. ax=1(%b) 求最小逆元

    定理一:如果d = gcd(a, b),则必能找到正的或负的整数x和y,使 d = a*x+ b*y. 定理二:若gcd(a, b) = ,则方程ax ≡ c (mod b)在[, b-]上有唯一解. ...

  4. 关于前台jsp页面的js取值问题

    在后程序中传一个字符串到前台页面上,后台代码model.addAttribute("ccc", "cccc"); 在页面js上用下面两种方法取值 1. var ...

  5. HDU 4871 Shortest-path tree 最短路 + 树分治

    题意: 输入一个带权的无向连通图 定义以顶点\(u\)为根的最短路生成树为: 树上任何点\(v\)到\(u\)的距离都是原图最短的,如果有多条最短路,取字典序最小的那条. 然后询问生成树上恰好包含\( ...

  6. base64转图片

    y一个简单的工具类,附上: /** * @param imgStr 图片的base64 * @param path 将要生成的地址 * @return */ public static boolean ...

  7. luogu1972 [SDOI2009]HH的项链

    莫队裸题还不带修改 #include <algorithm> #include <iostream> #include <cstdio> #include < ...

  8. luogu2756 飞行员配对方案问题

    匈牙利 #include <iostream> #include <cstring> #include <cstdio> using namespace std; ...

  9. webdriver高级应用- 使用Chrome浏览器自动将文件下载到指定路径

    #encoding=utf-8 from selenium import webdriver import unittest, time class TestDemo(unittest.TestCas ...

  10. python 查看异常

    接触python 一直觉着编译后报错经常没能捕捉显示,每次也只能从头看到尾 恰好在水木社区中看到关于异常捕捉帖子 方法一:捕获所有异常 try: a=b b=c except Exception,ex ...