10033 - Interpreter

题目

10033 - Interpreter
Time limit: 3.000 seconds

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.

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
1

299
492
495
399
492
495
399
283
279
689
078
100
000
000
000

Sample Output
16


解题思路


1. 模拟即可
2. 最后一个结果后面必须打印一个换行符
3. 设n是指令数,则空间复杂度为Θ(n)。时间复杂度没办法确定


通过代码

#include
#include
#include

int r[10];
char ram[1000][3];

int main(){
    int N;
    int I;
    int pc;

#ifdef DEBUG
    freopen("in","r",stdin);
    freopen("out","w",stdout);
#endif

    scanf("%d\n\n",&N);

    for(int i=0;imemset(r,0,sizeof(r));
        memset(ram,0,sizeof(ram));
        char str[5];
        for(I=0;gets(str)!=NULL;++I)
            if(strlen(str)!=0)
                strncpy(ram[I],str,3);
            else
                break;
        pc=0;
        bool stop=false;
        for(int j=0;!stop;++j){
            ++pc;
            int d1=ram[j][1]-'0';
            int d2=ram[j][2]-'0';
            switch(ram[j][0]){
                case '1':stop=true;break;
                case '2':r[d1]=d2;break;
                case '3':r[d1]+=d2;r[d1]%=1000;break;
                case '4':r[d1]*=d2;r[d1]%=1000;break;
                case '5':r[d1]=r[d2];break;
                case '6':r[d1]+=r[d2];r[d1]%=1000;break;
                case '7':r[d1]*=r[d2];r[d1]%=1000;break;
                case '8':r[d1]=ram[r[d2]][2]-'0';
                         r[d1]+=(ram[r[d2]][1]-'0')*10;
                         r[d1]+=(ram[r[d2]][0]-'0')*100;
                         break;
                case '9':ram[r[d2]][0]=r[d1]/100+'0';
                         ram[r[d2]][1]=(r[d1]/10)%10+'0';
                         ram[r[d2]][2]=r[d1]%10+'0';
                         break;
                case '0':if(r[d2]!=0) j=r[d1]-1;break;
            }
        }

        printf("%d\n",pc);
        if(i1)
            printf("\n");
    }
    return 0;
}


运行截图


这里写图片描述


我的发现


这里需要判定空行,使用gets(),而不使用scanf()。因为scanf()会忽略掉空行,继续读取空行后面的行

你可能感兴趣的:(算法,uva,C)