简略解题报告
Description
Input
Output
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 == ‘\n‘) return false; if (now == EOF) return false; while (now != ‘\n‘) { num = num*10 + now-‘0‘; now = getchar(); } memory[nowPosition] = num%1000; //printf("read %d (positon:%d[%d])\n", 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\n", 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\n", nextPosition, memory[nextPosition]); nowPosition = nextPosition; runedCommandNum++; } runedCommandNum++; printf("%d\n", runedCommandNum); } int main() { //int t; //scanf("%d", &t); //scanf("%*[ \n]"); //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; }
POJ 2577: Interpreter,布布扣,bubuko.com
原文:http://www.cnblogs.com/shinecheng/p/3577595.html