2013年9月15日 星期日

UVA 490 - Rotating Sentences

Problem link

Keyword: array manipulation

Algorithm:

After you get one line of input from stdin, put all characters into an array of strings.
For example, if this is your first line, put all letters into array[ n ][ 0 ], and array[ n ][ 1 ] for the second.
When the input is over, fill in blanks into each line.
By the way, the sample output in the problem is wrong. There should be a space after "r" line and "e" line.

Code:
#include<cstdio>
#include<cstring>
#include<algorithm>

int main()
{
    char out[ 100 ][ 101 ], in[ 100 ][ 101 ];
    int i = 0, max = 0;
    memset( out, 0, sizeof( out ) );
    memset( in, 0, sizeof( in ) );

    while( gets( in[ i ] ) )
        max = std::max( max, (int)strlen( in[ i++ ] ) );
    for( int k = i-1; k >= 0; --k )
        for( int l = 0; l < max; ++l )
            out[ l ][ i - k - 1 ] = in[ k ][ l ]? in[ k ][ l ] : ' ';
    for( int k = 0; k < max; ++k )
        puts( out[ k ] );

    return 0;
}

沒有留言:

張貼留言