学到两个:

//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语言版的更多相关文章

  1. PC/UVa 题号: 110105/10267 Graphical Editor (图形化编辑器)题解

    #include<cstdio> #include<iostream> #include<string> #include<algorithm> #in ...

  2. PC/UVa 题号: 110104/706 LC-Display (液晶显示屏)题解

    #include <string> #include <iostream> #include <cstring> #include <algorithm> ...

  3. 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 ...

  4. 23、Interpreter 解释器模式

    1.Interpreter 解释器模式 解释器模式是一种使用频率相对较低但学习难度较大的设计模式,它用于描述如何使用面向对象语言构成一个简单的语言解释器.在某些情况下,为了更好地描述某一些特定类型的问 ...

  5. 二十三、Interpreter 解释器模式

    设计: 代码清单: Node public abstract class Node { public abstract void parse(Context context) throws Parse ...

  6. 设计模式15:Interpreter 解释器模式(行为型模式)

    Interpreter 解释器模式(行为型模式) 动机(Motivation) 在软件构建过程中,如果某一特定领域的问题比较复杂,类似的模式不断重复出现,如果使用普通的编程方式来实现将面临非常频繁的变 ...

  7. uva题库爬取

    每次进uva都慢的要死,而且一步一步找到自己的那个题目简直要命. 于是,我想到做一个爬取uva题库,记录一下其中遇到的问题. 1.uva题目的链接是一个外部的,想要获取https资源,会报出SNIMi ...

  8. 【OCP|052】OCP最新题库解析(052)--小麦苗解答版

    [OCP|052]OCP最新题库解析(052)--小麦苗解答版 OCP最新题库解析历史连接(052):http://mp.weixin.qq.com/s/bUgn4-uciSndji_pUbLZfA ...

  9. emlog 百度熊掌号提交插件-基于Emlog6.0.1特别版美化

    插件截图 插件简介 此插件在发布文章的时候自动向百度熊掌号提交,有利于百度熊掌号收录.基于Emlog6.0.1特别版美化的插件. 在百度推送插件的基础上修改制作而成与百度推送共存,解放双手,走向人生巅 ...

随机推荐

  1. datatables 参数详解(转)

    //@translator codepiano //@blog codepiano //@email codepiano.li@gmail.com //尝试着翻译了一下,难免有错误的地方,欢迎发邮件告 ...

  2. Python用smtplib发送邮件

    参照了下面: 1. 先随便照着试试这个: http://blog.csdn.net/zhaoweikid/article/details/1638349 2. 这个写了一个很简洁的代码,看过NO.1就 ...

  3. 【多媒体封装格式详解】---MP4【4】

    前面介绍过的几种格式flv.mkv.asf等.他们音视频的数据包一般都是按照文件的顺序交叉安放.你解析完头部信息后.剩下的一般就按照文件顺序一个数据包一个数据包的解析就行了.但是MP4完全不是这种概念 ...

  4. function 中定义函数的默认返回值,

    result有默认值的int类型的为0,string类型的为‘’,tobject类型的为nil等等

  5. solr4.2 solrconfig.xml配置文件简单介绍

    对于solr4.x的每个core有两个很重要的配置文件:solrconfig.xml和schema.xml,下面我们来了解solrconfig.xml配置文件. 具体很详细的内容请细读solrcofi ...

  6. 强大的日志分析工具 -- NSLogger

    转:http://www.cnblogs.com/yingkong1987/p/3329945.html 强大的日志分析工具 -- NSLogger 源码:https://github.com/fpi ...

  7. 搞明白这八个问题,Linux系统就好学多了。

    正在犹豫入坑Linux学习的同学或者已经入坑的同学,经常会问到这样八个问题.今天,这些问题我都会一一解答,希望我的看法能帮助各位同学.常言道“好的开始是成功的一半”,如果你明白了以下八个问题,就能有一 ...

  8. HDU 5730 Shell Necklace cdq分治+FFT

    题意:一段长为 i 的项链有 a[i] 种装饰方式,问长度为n的相连共有多少种装饰方式 分析:采用dp做法,dp[i]=∑dp[j]*a[i-j]+a[i],(1<=j<=i-1) 然后对 ...

  9. AtCoder Grand Contest 001 C Shorten Diameter 树的直径知识

    链接:http://agc001.contest.atcoder.jp/tasks/agc001_c 题解(官方): We use the following well-known fact abou ...

  10. CSS、CSS2和CSS3选择器总结(全部选择器种类及其优先级)

    选择器种类罗列: 1.基础的选择器 选择器 含义 示例 * 通用元素选择器,匹配任何元素 * { margin:0; padding:0; } E 标签选择器,匹配所有使用E标签的元素 p { fon ...