poj2410

模拟

View Code
#include <iostream>
using namespace std;

const    int maxn=32;

int        byte[maxn];

void init()
{
    int        i;

    for (i=1;i<=31;i++)
        cin>>byte[i];
}

int num(int add)
{
    int        x=0,temp=1;
    
    while (add)
    {
        x+=add%10*temp;
        add/=10;
        temp*=2;
    }
    return x;
}

int    binary(int h)
{
    int        bin[9],x=0,i,ans=0;

    while (h)
    {
        bin[++x]=h%2;
        h/=2;
    }
    for (i=x;i>=1;i--)
        ans=ans*10+bin[i];
    return ans;
}

void inc(int &accu)
{
    int        i=1,temp;
    
    temp=num(accu)+1;
    if (temp>255)
        temp=0;
    accu=binary(temp);
}

void dec(int &accu)
{
    int        i=1,temp;
    
    temp=num(accu)-1;
    if (temp<0)
        temp=255;
    accu=binary(temp);
}

int work()
{
    int        accu=0,pc=0,x,ord;
    
    while (true)
    {
        x=num(byte[pc]%100000);
        ord=byte[pc]/100000;
        pc++;
        if (pc>31)
            pc=0;
        switch(ord)
        {
            case 0    :
                byte[x]=accu;
                break;
            case 1    :
                accu=byte[x];
                break;
            case 10    :
                pc=accu==0?x:pc;
                break;
            case 11    :
                break;
            case 100:
                dec(accu);
                break;
            case 101:
                inc(accu);
                break;
            case 110:
                pc=x;
                break;
            case 111:
                return accu;
                break;
        }
    }
}

void make(int x)
{
    int        temp=10000000;

    if (x==0)
    {
        cout<<"00000000\n";
        return;
    }
    while (x/temp==0)
    {
        cout<<"0";
        temp/=10;
    }
    cout<<x<<endl;
}

int main()
{
//    freopen("t.txt","r",stdin);
    while (cin>>byte[0])
    {
        init();
        make(work());
    }
    return 0;
}

 

 

posted @ 2013-01-15 16:06  金海峰  阅读(126)  评论(0编辑  收藏  举报