#UVa:10033-Interpreter

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

C++(0.008)

/*******************************************************/
/* UVa 10033 Interpreter                               */
/* Author: LanyiKnight [at] knightzone.org             */
/* 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 如何處理網站訪客的留言資料