若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1499: Interpreter

发表于 2017-10-06   |   分类于 HUSTOJ   |   阅读次数 1,250

题目描述

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:

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 1,000.

输入

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.

输出

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.

样例输入

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


样例输出

16


参考代码

#include"stdio.h"
#include"string.h"
int main() 
{
    int i=0,j=0,count=0;
    int t[1010],value[10];
    memset(value,0,sizeof(value));
    while(scanf("%d",&t[i++])!=EOF);
    while(++count) 
    {
        int a=t[j++];
        if(a==100)
                    break;
        int a3=a%10;
        int a2=a/10%10;
        int a1=a/100;
        switch(a1) 
        {
            case 2:value[a2]=a3;
            break;
            case 3:value[a2]+=a3;
            value[a2]%=1000;
            break;
            case 4:value[a2]*=a3;
            value[a2]%=1000;
            break;
            case 5:value[a2]=value[a3];
            break;
            case 6:value[a2]+=value[a3];
            value[a2]%=1000;
            break;
            case 7:value[a2]*=value[a3];
            value[a2]%=1000;
            break;
            case 8:value[a2]=t[value[a3]];
            break;
            case 9:t[value[a3]]=value[a2];
            break;
            case 0:if(value[a3]!=0)
                               j=value[a2];
            break;
        }
    }
    printf("%dn",count);
    return 0;
}

解析

暂无

hustoj

发表评论 取消回复

电子邮件地址不会被公开。 必填项已用*标注

*
*


hoxis wechat
著作权归作者所有
站点更新说明
  • 文章目录
  • 站点概览
若是凉夜已成梦

若是凉夜已成梦

青春里 总有些事情要努力去做 总有些梦想要拼命去追。

1904 日志
6 分类
12 标签
RSS
weibo github twitter facebook

友情链接

Skip 原站点 Dreams孤独患者
© 2017 若是凉夜已成梦
Powered by WordPress | 已运行
Theme By NexT.Mist