UVA 10033 - Interpreter
2017-09-01 08:22
246 查看
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
题意:给出一系列的指令,求总共执行的指令数;
思路:按照所给的指令进行模拟即可,要注意的是在输入第一个测试数的时候后面要跟一个空行,并且每两个测试用例之间要有一个空行;再有一个需要注意的是,虽然题目告诉我们RAM中的指令数只有1000条,但是不代表输入的指令数不超过1000条,所以数组RAM开的大一点,否则会出现Runtime Error;最后一点,在计算总共执行的指令数时,要加上停止指令的执行;
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.
Input
The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.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.
Output
For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.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.
Sample Input
1299
492
495
399
492
495
399
283
279
689
078
100
000
000
000
Sample Output
16题意:给出一系列的指令,求总共执行的指令数;
Instructions | Meaning |
---|---|
100 | 停止执行 |
2dn | 设置寄存器d的值为n(0-9) |
3dn | 寄存器d的值加上n |
4dn | 寄存器d的值乘上n |
5ds | 设置寄存器d的值为寄存器s的值 |
6ds | 寄存器d的值加上寄存器s的值 |
7ds | 寄存器d的值乘上寄存器s的值 |
8da | 设置寄存器d的值为RAM中地址为寄存器a的值 |
9sa | 设置RAM中地址为寄存器a的值为寄存器s的值 |
0ds | 跳转到RAM中地址为寄存器d的值处,除非此时寄存器s的值为0 |
#include <cstdio> #include <cstring> #include <memory> int RAM[10000]; int reg[10+5]; #define op1(num) (num/100) #define op2(num) (num/10%10) #define op3(num) (num%10) int execute(){ int cur = 0; //the position of RAM int cnt = 0+1; //the number of instructions executed int op; //current instruction while(1){ op = RAM[cur++]; switch(op1(op)){ case 0: if(reg[op3(op)]) cur = reg[op2(op)]; break; case 1: if(!op2(op) && !op3(op)) return cnt; break; case 2: reg[op2(op)] = op3(op); break; case 3: reg[op2(op)] = (reg[op2(op)] + op3(op))%1000; break; case 4: reg[op2(op)] = (reg[op2(op)] * op3(op))%1000; break; case 5: reg[op2(op)] = reg[op3(op)]; break; case 6: reg[op2(op)] = (reg[op2(op)] + reg[op3(op)])%1000; break; case 7: reg[op2(op)] = (reg[op2(op)] * reg[op3(op)])%1000; break; case 8: reg[op2(op)] = RAM[reg[op3(op)]]; break; case 9: RAM[reg[op3(op)]] = reg[op2(op)]; break; default: break; } cnt++; } } int main(){ int N; scanf("%d\n\n", &N); while(N--){ char buff[10]; int cnt = 0; memset(RAM, 0, sizeof(RAM)); memset(reg, 0, sizeof(reg)); while(gets(buff)){ if(!strcmp(buff, "")) break; sscanf(buff, "%d", RAM+cnt); cnt++; } printf("%d\n", execute()); if(N) printf("\n"); } return 0; }
相关文章推荐
- UVa Problem Solution: 10033 - Interpreter
- UVa 10033 - Interpreter
- UVa Problem 10033 Interpreter (解释器)
- UVa 10033 - Interpreter
- UVa 10033 Interpreter
- UVA 10033 Interpreter (模拟)
- UVa 10033 - Interpreter
- UVa Problem 10033 Interpreter (解释器)
- 10033 - Interpreter
- UVA 10033
- 10033 - Interpreter
- PC/UVa 题号: 110106/10033 Interpreter (解释器)题解 c语言版
- UVA 275 - Expanding Fractions
- UVA 116 - Unidirectional TSP
- 数学专项counting:UVa 10883
- Uva 11802
- (beginer)DFS (强连通)UVA 11504 Dominos
- Regionals 2007 >> Europe - Southwestern Ladies' Choice 稳定婚姻问题 uva live 3989
- UVA 1658 Admiral(拆点+费用流)
- uva 10162 - Last Digit(数论)