Interpreter 본문

Programming/Questions

Interpreter

halatha 2009. 10. 29. 14:27
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.

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

- time limit exceed 발생 ㅜ.ㅜ
- scanf를 이용해 공백 라인을 읽는 방법 참고

- time limit exceed의 원인은 switch()의 default:에서 iPC++을 안 넣어줘서
- iNumOfCases의 앞에 빈 printf("\n");는 output 형식을 맞추기 위함 ->없으면 presentation error

#include 
#include 

#define	REG_SIZE	10
#define	RAM_SIZE	1000
#define	CMD_SIZE	4

int main()
{
	int	i, j, d, n, s, a, iRC, iNumOfCases, Reg[REG_SIZE], iRAMNum, iPC, iNumOfIns;
	char	pcLine[CMD_SIZE], RAM[RAM_SIZE][CMD_SIZE];

	iRC	= scanf("%d%*[^\n]", &iNumOfCases);
	if ( iRC < 0 )
		return	1;
	getchar();	//	remove '\n' at the end of iNumOfCases
	getchar();	//	remove blank line between iNumOfCases and first instruction
	//printf("NumOfCases: %d\n", iNumOfCases);
	for ( i = 0; i < iNumOfCases; ++i )
	{
		//printf("------------------------------\n");

		//	initialize register & RAM
		memset(Reg, '\0', sizeof(int) * REG_SIZE);
		memset(RAM, '\0', sizeof(char) * RAM_SIZE * CMD_SIZE);

		iRAMNum	= 0;
		memset(pcLine, '\0', CMD_SIZE);
		while ( ( iRC = scanf("%4[^\n]%*[^\n]", pcLine) ) >= 0 )
		{
			if ( iRC >= 0 )	getchar();	//	remove '\n' at the end of pcLine

			if ( 0 == strlen(pcLine) )	//	blank line between instruction sets
				break;	//	goto next instructions set

			//	copy instructions into RAM
			strncpy(RAM[iRAMNum], pcLine, sizeof(char) * CMD_SIZE);
			//printf("%s\n", pcLine);
			//printf("RAM[%d] = %s\n", iRAMNum, RAM[iRAMNum]);
			++iRAMNum;
			memset(pcLine, '\0', CMD_SIZE);
		}

		iPC	= 0;	iNumOfIns	= 0;
		while ( 1 )
		{
			if ( 0 == strcmp(RAM[iPC], "100") )
			{
				++iNumOfIns;
				//printf("halt\n");
				break;
			}
			switch ( RAM[iPC][0] )
			{
				case '2':
					d	= RAM[iPC][1] - '0';	n	= RAM[iPC][2] - '0';
					Reg[d]	= n;
					++iPC;
					break;
				case '3':
					d	= RAM[iPC][1] - '0';	n	= RAM[iPC][2] - '0';
					Reg[d]	+= n;	Reg[d]	%= 1000;
					++iPC;
					break;
				case '4':
					d	= RAM[iPC][1] - '0';	n	= RAM[iPC][2] - '0';
					Reg[d]	*= n;	Reg[d]	%= 1000;
					++iPC;
					break;
				case '5':
					d	= RAM[iPC][1] - '0';	s	= RAM[iPC][2] - '0';
					Reg[d]	= Reg[s];
					++iPC;
					break;
				case '6':
					d	= RAM[iPC][1] - '0';	s	= RAM[iPC][2] - '0';
					Reg[d]	+= Reg[s];	Reg[d]	%= 1000;
					++iPC;
					break;
				case '7':
					d	= RAM[iPC][1] - '0';	s	= RAM[iPC][2] - '0';
					Reg[d]	*= Reg[s];	Reg[d]	%= 1000;
					++iPC;
					break;
				case '8':
					d	= RAM[iPC][1] - '0';	a	= RAM[iPC][2] - '0';
					Reg[d]	= atoi(RAM[Reg[a]]);
					++iPC;
					break;
				case '9':
					s	= RAM[iPC][1] - '0';	a	= RAM[iPC][2] - '0';
					sprintf(RAM[Reg[a]], "%d", Reg[s]);
					++iPC;
					break;
				case '0':
					d	= RAM[iPC][1] - '0';	s	= RAM[iPC][2] - '0';
					if ( Reg[s] != 0 )	iPC	= Reg[d];
					else				++iPC;
					break;
				default:
					++iPC;
					break;
			}
			++iNumOfIns;
			//for ( j = 0; j < REG_SIZE; ++j )
			//	printf("%d|", Reg[j]);
			//printf("\n");
		}
		if ( 0 < i )	printf("\n");
		printf("%d\n", iNumOfIns);
	}

	return	0;
}
Comments