2013年9月15日 星期日

UVA 445 - Marvelous Mazes

Problem link

Keyword: string processing

Algorithm:

Scan the quantity required and the letter specified and print them.
Remember "!" means newline and "b" for blank.

Note:

1. The output length of each line is 132, and you shouldn't print extra letters, even though there may be more than 132 letters in one line.
2. There may be numbers such as 1111111111111, and therefore you can't use scanf %d to see how many letters are needed.

Code:
#include<cctype>
#include<iostream>
using namespace std;
void transform( int &n )
{
    n = 0;
    while( isdigit( cin.peek() ) )
        n += cin.get() - '0';
}

int main()
{
    char status;
    int quant = 0, line = 0;

    while( isprint( status = cin.peek() ) || status == '\n' )
        if( isdigit( status ) )
        {
            transform( quant );
            cin >> status;
            if( line + quant > 132 )
                quant = 132 - line;
            line += quant;
            if( status == 'b' )
                status = ' ';
            for( int i = 0; i < quant; ++i )
                cout << status;
        }
        else if( status == '!' || status == '\n' )
        {
            cin.get();
            cout << endl;
            line = 0;
        }

    return 0;
}

沒有留言:

張貼留言