2014年1月28日 星期二

UVa 11956 - Brainfuck

Problem link

Keyword: ad hoc

Algorithm:

This problem needs knowledge of pointer manipulation.
If you're not familiar with it, you may want to read some materials first.

There are 4 functions, 2 with pointer addition/subtraction and 2 with dereferencing.
Implement those 4 ones and you're done with the manipulation part.

You can use printf( "%02X" ); to print hexadecimal numbers.

Code:
#include<cstdio>

int main()
{
    int t;
    char str[ 100000 ];
    scanf( "%d\n", &t );

    for( int i = 1; i <= t; ++i )
    {
        unsigned char val[ 100000 ] = { 0 }, *ptr = val;
        gets( str );
        printf( "Case %d:", i );
        for( int k = 0; str[ k ]; ++k )
            if( str[ k ] == '>' )
                ptr = ptr == val+99? val : ptr+1;
            else if( str[ k ] == '<' )
                ptr = ptr == val? val+99 : ptr-1;
            else if( str[ k ] == '+' )
                ++*ptr;
            else if( str[ k ] == '-' )
                --*ptr;
        for( int i = 0; i < 100; ++i )
            printf( " %02X", val[ i ] );
        puts( "" );
    }

    return 0;
}

沒有留言:

張貼留言