#UVa:10033-Interpreter

灆洢 2012-11-10 12:33:39

照題目要求去做處理即可得解。

C++(0.008)

/*******************************************************/
/* UVa 10033 Interpreter                               */
/* Author: Maplewing [at] knightzone.studio            */
/* Version: 2012/11/10                                 */
/*******************************************************/
#include <iostream>
#include <cstdio>
using namespace std;

int main(){
  int n;

  while( scanf( "%d", &n ) != EOF ){
    getchar();
    getchar();
    for( int i = 0 ; i < n ; i++ ){
      int registers[10] = {0}, pointer = 0, RAM[1000] = {0};
      string input;

      while( getline( cin, input ) && input != "" )
        sscanf( input.c_str(), "%d", &RAM[pointer++] );

      int execute = 1, opcode, dest, source;
      pointer = 0;

      while( RAM[pointer] != 100 ){
        opcode = RAM[pointer] / 100;
        dest = RAM[pointer] / 10 % 10;
        source = RAM[pointer] % 10;
        pointer++;
        execute++;

        switch( opcode ){
          case 2:
            registers[dest] = source;
            break;
          case 3:
            registers[dest] += source;
            break;
          case 4:
            registers[dest] *= source;
            break;
          case 5:
            registers[dest] = registers[source];
            break;
          case 6:
            registers[dest] += registers[source];
            break;
          case 7:
            registers[dest] *= registers[source];
            break;
          case 8:
            registers[dest] = RAM[registers[source]];
            break;
          case 9:
            RAM[registers[source]] = registers[dest];
            break;
          case 0:
            if( registers[source] ) pointer = registers[dest];
            break;
          default:
            break;
        } 
        registers[dest] %= 1000;
      }

      if( i ) printf( "\n" );
      printf( "%d\n", execute );
    }
  }

  return 0;
}

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步瞭解 Akismet 如何處理網站訪客的留言資料