1.6.6 解释器 Interpreter

PC/UVA 110106/10033

本题。。模拟即可。

//author: CHC
//First Edit Time:	2014-01-12 22:00
//Last Edit Time:	2014-01-13 11:30
//Filename:1.cpp
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int n,ord[2000],sum,re[10];
char s[2000];
int main()
{
    scanf("%d",&n);
    getchar();
    getchar();
    //fgets(s,sizeof(s),stdin);
    while(n--)
    {
        int ns=0;
        memset(ord,0,sizeof(ord));
        memset(re,0,sizeof(re));
        //while(fgets(s,sizeof(s),stdin)!=NULL&&s[0]!='\n')
        while(gets(s)!=NULL)
        {
            if(strcmp(s,"")==0)break;
            sscanf(s,"%d",&ord[ns]);
            ++ns;
        }
        sum=0;
        for(int i=0;;i++)
        {
            ++sum;
            /*
            for(int j=0;j<10;j++)
                printf("%d:%d\t",j,re[j]);
            puts("");
            printf("%d\n",ord[i]);
            puts("");
            */
            int t=ord[i]/100,tg=ord[i]%10,ts=(ord[i]%100)/10;
            ord[i]%=1000;
            if(t==0&&re[tg]) i=re[ts]-1;
            else if(t==1) break;
            else if(t==2) re[ts]=tg;
            else if(t==3) re[ts]=(re[ts]+tg)%1000;
            else if(t==4) re[ts]=(re[ts]*tg)%1000;
            else if(t==5) re[ts]=re[tg];
            else if(t==6) re[ts]=(re[ts]+re[tg])%1000;
            else if(t==7) re[ts]=(re[ts]*re[tg])%1000;
            else if(t==8) re[ts]=ord[re[tg]]%1000;
            else if(t==9) ord[re[tg]]=re[ts]%1000;
            /*
            for(int j=0;j<10;j++)
                printf("%d:%d\t",j,re[j]);
            puts("");
            system("pause");
            */
        }
        printf("%d\n",sum);
        if(n)puts("");
    }
    return 0;
}

Interpreter

 

A certain computer has ten registers and 1,000 words of RAM. Each register or RAM location holds a three-digit integer between 0 and 999. Instructions are encoded as three-digit integers and stored in RAM. The encodings are as follows:

100means halt
2dnmeans set register d to n (between 0 and 9)
3dnmeans add n to register d
4dnmeans multiply register d by n
5dsmeans set register d to the value of registers
6dsmeans add the value of register s to registerd
7dsmeans multiply register d by the value of register s
8dameans set register d to the value in RAM whose address is in register a
9sameans set the value in RAM whose address is in register a to the value of register s
0dsmeans 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 1,000.

Input

The input begins with a single positive integer on a line by itself indicating the number of cases, each described as below. This is followed by a blank line, and there will be a blank line between each two consecutive inputs.

Each input case consists of up to 1,000 three-digit unsigned integers, representing the contents of consecutive RAM locations starting at 0. Unspecified RAM locations are initialized to 000.

Output

The output of each test case is a single integer: the number of instructions executed up to and including the halt instruction. You may assume that the program does halt. Separate the output of two consecutive cases by a blank line.

Sample Input

1

299
492
495
399
492
495
399
283
279
689
078
100
000
000
000

Sample Output

16

 
    

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值