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 ...
随机推荐
- 【线段树合并】bzoj3545: [ONTAK2010]Peaks
1A还行 Description 在Bytemountains有N座山峰,每座山峰有他的高度h_i.有些山峰之间有双向道路相连,共M条路径,每条路径有一个困难值,这个值越大表示越难走,现在有Q组询问, ...
- 【概率dp 高斯消元】bzoj3270: 博物馆
一类成环概率dp的操作模式 Description 有一天Petya和他的朋友Vasya在进行他们众多旅行中的一次旅行,他们决定去参观一座城堡博物馆.这座博物馆有着特别的样式.它包含由m条走廊连接的n ...
- 【linux】【安全】服务器安全建议
引用自 <鸟哥的linux私房菜-服务器篇> http://cn.linux.vbird.org/linux_server/0210network-secure_1.php 建立完善的登 ...
- 下载旧版本的JDK
下载旧版本的JDK 有的时候我们需要去下载旧版本的JDK,但是进入Oracle官网,显示的总是新版的JDK,这里告诉大家怎么样去下载旧版本的JDK. 首先去JavaSE的 下载界面 拉到最下面,找到这 ...
- Django ORM (三) 查询,删除,更新操作
ORM 查询操作 修改 views.py 文件 from django.shortcuts import render, HttpResponse from app01 import models f ...
- Python中的可迭代对象,迭代器与生成器
先来看一张概览图,关于容器(container).可迭代对象(Iterable).迭代器(iterator).生成器(generator). 一.容器(container) 容器就是一个用来存储多个元 ...
- LeetCode(234) Palindrome Linked List
题目 Given a singly linked list, determine if it is a palindrome. Follow up: Could you do it in O(n) t ...
- Windows7_64位 NVIDIA 卡 OpenCl环境配置
序 最近做一个项目需要用到OpenCL,由于之前没有接触过,所以在环境配置第一关就遇到了一些问题,查阅很多资料才配置完成,现在记录如下,希望给一些童鞋一些帮助. 整个步骤也很简单: 了解系统配置,选择 ...
- 中国首届CSS开发者大会讲师照片
中国首届CSS开发者大会讲师照片 Bert Bos Winter 点头猪 灭灭 jaychsu Hax 尤雨溪 一丝 勾三股四 小倩 **
- Question | 移动端虚拟机注册等作弊行为的破解之道
本文来自网易云社区 "Question"为网易云易盾的问答栏目,将会解答和呈现安全领域大家常见的问题和困惑.如果你有什么疑惑,也欢迎通过邮件(zhangyong02@corp.ne ...