zoukankan      html  css  js  c++  java
  • A

    A - Interpreter
    Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu
    Submit Status
    Appoint description: 

    Description

    Download as PDF
     

    Problem G: Interpreter


    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
    /*
     * Author:  
     * Created Time:  2013/10/4 20:16:26
     * File Name: A.cpp
     * solve: A.cpp
     */
    #include<cstdio>
    #include<cstring>
    #include<cstdlib>
    #include<cmath>
    #include<algorithm>
    #include<string>
    #include<map>
    #include<stack>
    #include<set>
    #include<iostream>
    #include<vector>
    #include<queue>
    //ios_base::sync_with_stdio(false);
    //#pragma comment(linker, "/STACK:1024000000,1024000000")
    
    using namespace std;
    #define sz(v) ((int)(v).size())
    #define rep(i, a, b) for (int i = (a); i < (b); ++i)
    #define repf(i, a, b) for (int i = (a); i <= (b); ++i)
    #define repd(i, a, b) for (int i = (a); i >= (b); --i)
    #define clr(x) memset(x,0,sizeof(x))
    #define clrs( x , y ) memset(x,y,sizeof(x))
    #define out(x) printf(#x" %d
    ", x)
    #define sqr(x) ((x) * (x))
    typedef long long LL;
    
    const int INF = 1000000000;
    const double eps = 1e-8;
    const int maxn = 30000;
    
    int sgn(const double &x) {  return (x > eps) - (x < -eps); }
    
    int r[20];
    char str[100];
    int addr[1100];
    
    int main() 
    {
        //freopen("in.txt","r",stdin);
        int T;
        scanf("%d",&T);
        getchar();
        getchar();
        int first = 0;
        while(T--)
        {
            if(first)
                cout<<endl;
            first = 1;
    
            clr(r);
            clr(addr);
            int num = 0;
            while(gets(str)&& str[0] != '')
            {
                int a = str[0] - '0';
                int b = str[1] - '0';
                int c = str[2] - '0';
                int temp = a*100 + b*10 + c;
                addr[num] = temp;
                num++;
            }
            int ans = 0;
            int i = 0;
            while(i<1000)
            {
                int a = addr[i]/100;
                int b = (addr[i]/10)%10;
                int c = addr[i]%10;
                switch(a)
                {
                    case 2 : r[b] = c;r[b] %= 1000;break;
                    case 3 : r[b] += c;r[b] %= 1000;break;
                    case 4 : r[b] *= c;r[b] %= 1000;break;
                    case 5 : r[b] = r[c];r[b] %= 1000;break;
                    case 6 : r[b] += r[c];r[b] %= 1000; break;
                    case 7 : r[b] *= r[c];r[b] %= 1000;break;
                    case 8 : r[b] = addr[r[c]];r[b] %= 1000;break;
                    case 9 : addr[r[c]] = r[b];break;
                    case 0 : if(r[c]) i = r[b] - 1;break;
                    default: //if(b == 0 && c == 0)
                            // {
                                 ans++;
                                 goto here;
                            // }
                             break;
                }
                i++;
                ans++;
            }
    here:
            cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    逻辑运算符&逻辑短路
    python运算符&优先性
    python类型强转&二级容器
    python中转义符&str格式化
    python中变量的缓存机制
    底层二进制的计算
    python容器数据类型的特色
    进制的转化
    shell脚本学习(1)-菜鸟教程
    python中yield的用法-简单明了!
  • 原文地址:https://www.cnblogs.com/DreamHighWithMe/p/3371262.html
Copyright © 2011-2022 走看看