简略解题报告
Description
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:
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.
- 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 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
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
299 492 495 399 492 495 399 283 279 689 078 100 000 000 000
Sample Output
16
//POJ 2577 //题意:按题意实现一个解释器。模拟以前的某种计算机的CPU吧 //题型:简单模拟题 //思路:有条理就行 #include <cstdio> #include <cstring> int hotal[10]; int memory[1000]; int runedCommandNum; //读入本行数字,储存在对应内存位置 //若为空行,返回false bool readIntInThisLine(int nowPosition) { char now; int num = 0; now = getchar(); if (now == ' ') return false; if (now == EOF) return false; while (now != ' ') { num = num*10 + now-'0'; now = getchar(); } memory[nowPosition] = num%1000; //printf("read %d (positon:%d[%d]) ", num%1000, nowPosition, memory[nowPosition]); return true; } //执行命令 //描述:从指定内存处执行命令,并通过参数返回下一条命令所在内存。 // 如果停机,返回false bool runCommandAt(int nowPosition, int &nextPosition) { char command[10]; sprintf(command, "%03d", memory[nowPosition]); //printf("command = %s ", command); nextPosition = nowPosition+1; switch (command[0]) { case '1': //如果后面不是00,那是什么命令 if (command[1] == '0' && command[2] == '0') return false; else return true; case '2': hotal[command[1]-'0'] = command[2]-'0'; hotal[command[1]-'0'] %= 1000; return true; case '3': hotal[command[1]-'0'] += command[2]-'0'; hotal[command[1]-'0'] %= 1000; return true; case '4': hotal[command[1]-'0'] *= command[2]-'0'; hotal[command[1]-'0'] %= 1000; return true; case '5': hotal[command[1]-'0'] = hotal[command[2]-'0']; hotal[command[1]-'0'] %= 1000; return true; case '6': hotal[command[1]-'0'] += hotal[command[2]-'0']; hotal[command[1]-'0'] %= 1000; return true; case '7': hotal[command[1]-'0'] *= hotal[command[2]-'0']; hotal[command[1]-'0'] %= 1000; return true; case '8': hotal[command[1]-'0'] = memory[hotal[command[2]-'0']]; hotal[command[1]-'0'] %= 1000; return true; case '9': memory[hotal[command[2]-'0']] = hotal[command[1]-'0'] ; memory[hotal[command[2]-'0']] %= 1000; return true; case '0': if (hotal[command[2]-'0'] != 0) { nextPosition = hotal[command[1]-'0']; } return true; } } // 开机 // 描述:开机运行命令,停机后输出命令数 void run() { int nowPosition = 0; int nextPosition; runedCommandNum = 0; while (runCommandAt(nowPosition, nextPosition)) { //printf("nextPosition = %d, command = %03d ", nextPosition, memory[nextPosition]); nowPosition = nextPosition; runedCommandNum++; } runedCommandNum++; printf("%d ", runedCommandNum); } int main() { //int t; //scanf("%d", &t); //scanf("%*[ ]"); //while (t--) { // memset(memory, 0, sizeof(memory)); // memset(hotal, 0, sizeof(hotal)); // int nowPosition = 0; // while(readIntInThisLine(nowPosition) == true) nowPosition++; // run(); //} int n; memset(memory, 0, sizeof(memory)); memset(hotal, 0, sizeof(hotal)); int now = 0; while (scanf("%d", &n) != EOF) { memory[now++] = n%1000; } run(); return 0; }