Interpreter

Interpreter

时间: 1ms        内存:64M

描述:

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

提示:

参考答案(内存最优[752]):

#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("%d\n",count);
	return 0;
}

参考答案(时间最优[0]):

#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("%d\n",count);
	return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

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