若是凉夜已成梦

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


  • 运维

  • 前端

  • 编程

  • 随笔

  • hust-oj

1109: Problem E: Interpreter

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

题目描述

Problem E: Interpreter
A certain computer has 10 registers and 1000 words of RAM. Each register or RAM location holds a 3-digit integer between 0 and 999. Instructions are encoded as 3-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 1000.
The 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.
The 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.

输入

暂无

输出

暂无

样例输入

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

样例输出

16

参考代码

#include <stdio.h>
int ram[1000];
int r[10];
int i,ip,a,b,c;
main() 
{
    while (1 == scanf("%d",&ram[i++])) 
    {
    }
    for (i=1;;i++) 
    {
        a = ram[ip]/100;
        b = ram[ip]/10%10;
        c = ram[ip]%10;
        ip++;
        if (a == 1) break;
        if (a == 2) r[b] = c; else if (a == 3) r[b] = (r[b]+c)%1000; else if (a == 4) r[b] = (r[b]*c)%1000; else if (a == 5) r[b] = r[c]; else if (a == 6) r[b] = (r[b]+r[c])%1000; else if (a == 7) r[b] = (r[b]*r[c])%1000; else if (a == 8) r[b] = ram[r[c]]; else if (a == 9) ram[r[c]] = r[b]; else if (a == 0 && r[c] !=0) ip = r[b];
    }
    printf("%dn",i);
}

解析

暂无

hustoj

发表评论 取消回复

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

*
*


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

若是凉夜已成梦

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

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

友情链接

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