PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版
学到两个:
//ignore \n
cin.ignore();
//ignore a line
cin.ignore(1024, '\n');
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<iterator>
#include<cstring> //uva 10033 Problem G: Interpreter
#define ONLINE_JUDGE using namespace std;
int regs[10];
int ram[1000]; int steps;
int pc=0;
int oper, arg1, arg2; void load_ram()
{
steps=0;
pc=0;
memset(regs, 0 ,sizeof(regs));
memset(ram, 0 ,sizeof(ram));
//string s;
char s[5];
int i=0;
//for(int i=0;getline(cin, s)&&s.length();i++)
for(int i=0;fgets(s, sizeof(s), stdin);i++)
{
if(s[0]=='\n')
{
// printf("\\n \n");
break;
}
//ram[i]=atoi(s.c_str());
ram[i]=atoi(s);
#ifndef ONLINE_JUDGE
cout<<ram[i]<<endl;
#endif
} } void decode()
{
oper=ram[pc]/100;
arg1=ram[pc]/10%10;
arg2=ram[pc]%10;
pc++; steps++;
#ifndef ONLINE_JUDGE
printf("steps %d pc %d: %d %d %d\n", steps, pc-1, oper, arg1, arg2);
#endif } void run()
{
while(1)
{
decode();
switch(oper)
{
//halt
case 1:
goto END;
break;
//2dn means set register d to n (between 0 and 9)
case 2:
regs[arg1]=arg2;
break;
//3dn means add n to register d
case 3:
regs[arg1]+=arg2;
regs[arg1]%=1000;
break;
//4dn means multiply register d by n
case 4:
regs[arg1]*=arg2;
regs[arg1]%=1000;
break;
//5ds means set register d to the value of register s
case 5:
regs[arg1]=regs[arg2];
break;
//6ds means add the value of register s to register d
case 6:
regs[arg1]+=regs[arg2];
regs[arg1]%=1000;
break;
//7ds means multiply register d by the value of register s
case 7:
regs[arg1]*=regs[arg2];
regs[arg1]%=1000;
break;
//8da means set register d to the value in RAM whose address is in register a
case 8:
regs[arg1]=ram[regs[arg2]];
break;
//9sa means set the value in RAM whose address is in register a to the value of register s
case 9:
ram[regs[arg2]]=regs[arg1];
break;
//0ds means goto the location in register d unless register s contains 0
case 0:
if(regs[arg2]!=0)
pc=regs[arg1];
break;
default:
break;
} }
END:
return; } int main()
{
int n;
bool first=true;
char instr[5]={0};
//cin>>n;
scanf("%d", &n);
//ignore \n
//cin.ignore();
fgets(instr, sizeof(instr), stdin);
memset(instr, 0, sizeof(instr)); //ignore a line
//cin.ignore(1024, '\n');
fgets(instr, sizeof(instr), stdin);
while(n-- >0)
{
load_ram();
run(); if(!first)
printf("\n");
printf("%d\n", steps);
first=false;
} return 0;
}
PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版的更多相关文章
- PC/UVa 题号: 110105/10267 Graphical Editor (图形化编辑器)题解
#include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...
- PC/UVa 题号: 110104/706 LC-Display (液晶显示屏)题解
#include <string> #include <iostream> #include <cstring> #include <algorithm> ...
- PC/UVa 题号: 110101/100 The 3n+1 problem (3n+1 问题)
The 3n + 1 problem Background Problems in Computer Science are often classified as belonging to a ...
- 23、Interpreter 解释器模式
1.Interpreter 解释器模式 解释器模式是一种使用频率相对较低但学习难度较大的设计模式,它用于描述如何使用面向对象语言构成一个简单的语言解释器.在某些情况下,为了更好地描述某一些特定类型的问 ...
- 二十三、Interpreter 解释器模式
设计: 代码清单: Node public abstract class Node { public abstract void parse(Context context) throws Parse ...
- 设计模式15:Interpreter 解释器模式(行为型模式)
Interpreter 解释器模式(行为型模式) 动机(Motivation) 在软件构建过程中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变 ...
- uva题库爬取
每次进uva都慢的要死,而且一步一步找到自己的那个题目简直要命. 于是,我想到做一个爬取uva题库,记录一下其中遇到的问题. 1.uva题目的链接是一个外部的,想要获取https资源,会报出SNIMi ...
- 【OCP|052】OCP最新题库解析(052)--小麦苗解答版
[OCP|052]OCP最新题库解析(052)--小麦苗解答版 OCP最新题库解析历史连接(052):http://mp.weixin.qq.com/s/bUgn4-uciSndji_pUbLZfA ...
- emlog 百度熊掌号提交插件-基于Emlog6.0.1特别版美化
插件截图 插件简介 此插件在发布文章的时候自动向百度熊掌号提交,有利于百度熊掌号收录.基于Emlog6.0.1特别版美化的插件. 在百度推送插件的基础上修改制作而成与百度推送共存,解放双手,走向人生巅 ...
随机推荐
- angularjs指令中的compile与link函数详解(转)
http://www.jb51.net/article/58229.htm 通常大家在使用ng中的指令的时候,用的链接函数最多的是link属性,下面这篇文章将告诉大家complie,pre-link, ...
- apache开源项目 -- tez
为了更高效地运行存在依赖关系的作业(比如Pig和Hive产生的MapReduce作业),减少磁盘和网络IO,Hortonworks开发了DAG计 算框架Tez.Tez是从MapReduce计算框架演化 ...
- noip2008提高组题解
第一题:笨小猴 模拟 第二题:火柴棒等式 搜索 深搜不用说,确定出两个加数然后判断能否拼出等式. 枚举确实不太好搞,因为枚举范围不确定,太大了容易超时,太小了容易漏解.不过这题的数据貌似很温和,我 ...
- android adt与android sdk有什么关系,他们在开发中各起到什么作用
ADT(Android Development Tools):目前Android开发所用的开发工具是Eclipse,在Eclipse编译IDE环境中,安装ADT,为Android开发提供开发工具的升级 ...
- Jedis的JedisSentinelPool源代码分析
概述 Jedis是Redis官方推荐的Java客户端,更多Redis的客户端可以参考Redis官网客户端列表.Redis-Sentinel作为官方推荐的HA解决方案,Jedis也在客户端角度实现了对S ...
- C#获取汉字拼音
C#获取汉字拼音 using System; using System.Collections.Generic; using System.Text; using System.Text.Reg ...
- ASM基本操作
1. 添加一个磁盘组 SQL> create diskgroup recover external redundancy disk 'ORCL:kel3'; Diskgroup created. ...
- 百家搜索:在网站中添加Google、百度等搜索引擎
来源:http://www.ido321.com/1143.html 看到一些网站上添加了各种搜索引擎.如Google.百度.360.有道等,就有点好奇,这个怎么实现?研究了一各个搜索引擎怎么传送关键 ...
- 2016年CCF第七次测试 俄罗斯方块
//2016年CCF第七次测试 俄罗斯方块 // 这道小模拟题还是不错 // 思路:处理出输入矩阵中含1格子的行数和列数 // 再判是否有一个格子碰到底部,否则整体再往下移动一步,如果有一个格子不能移 ...
- 解决oracle启动的错误: ORA-00119 ORA-00132
$ sqlplus / as sysdbaSQL>create pfile fromspfile修改$ORACLE_HOME/dbs下的文件initDEV.ora中的*.local_listen ...