2013年9月15日 星期日

UVA 414 - Machined Surfaces

Problem link

Keyword: ad hoc

Algorithm:

Scan through all lines and count how many spaces are there in each line.
After this, find the line with least spaces and subtract this length from every line, and you'll get the answer.

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

int main()
{
    int n;
    char pic[ 12 ][ 26 ];

    while( scanf( "%d\n", &n ) && n )
    {
        int blank[ 12 ] = { 0 }, total = 0, least = 30;
        for( int i = 0; i < n; ++i )
        {
            gets( pic[ i ] );
            for( int k = 0; k < 25; ++k )
                if( pic[ i ][ k ] == ' ' )
                    ++blank[ i ];
            least = std::min( least, blank[ i ] );
            total += blank [ i ];
        }
        printf( "%d\n", total - n*least );
    }

    return 0;
}

沒有留言:

張貼留言