Message Decoding

UVa213

这道题对练习操作挺不错的,将字符保存到二维数组中就行了。

#include <stdio.h>
#include <string.h>
const int maxn = 8;
char str[maxn][1 << maxn];
char ch[1 << maxn];
char readchar ( )
{
    while ( 1 )
    {
        char ch = getchar ( );
        if ( ch != '\r' && ch != '\n' )
            return ch;
    }
}
int readint ( int len )
{
    int v = 0;
    while ( len -- )
    {
        char ch = readchar ( );
        v = v*2+( ch-'0' );
    }
    return v;
}
void print ( )
{
    for ( int i = 1; i < maxn; i ++ )
    {
        int n = ( 1 << i )-1;
        printf ( "%d:", i );
        for ( int j = 0; j < n; j ++ )
            printf ( "%c", str[i][j] );
        printf ( "\n" );
    }
}
int readcodes ( )
{
    memset ( str, 0, sizeof ( str ) );
    str[1][0] = readchar ( );   //先将前面的换行全部去掉
    for ( int i = 2; i < maxn; i ++ )
    {
        int n = ( 1 << i )-1;   //0-2^i-2
        for ( int j = 0; j < n; j ++ )
        {
            char ch = getchar ( );  //然后一个个读
            if ( ch == EOF )
                return 0;   //结束
            if ( ch == '\r' || ch == '\n' )
                return 1;   //收到换行就返回1
            str[i][j] = ch;
        }
    }
}
int main ( )
{
    //freopen ( "in0.in", "r", stdin );
    while ( readcodes ( ) )
    {
        //print ( );
        while ( 1 )
        {
            int len = readint ( 3 );
            //读一个长度
            if ( len == 0 ) //当读取的长度为0退出
                break ;
            while ( 1 )
            {
                int v = readint ( len );    //读取长度len个字符
                if ( v == ( 1 << len )-1 )
                    break ;
                putchar ( str[len][v] );
            }
        }
        printf ( "\n" );
    }
    return 0;
}


你可能感兴趣的:(Message Decoding)