1. 程式人生 > >UVA445 Marvelous Mazes【輸入輸出+水題】

UVA445 Marvelous Mazes【輸入輸出+水題】

Your mission, if you decide to accept it, is to create a maze drawing program. A maze will consist ofthe alphabetic characters A-Z, * (asterisk), and spaces.

Input

Your program will get the information for the mazes from the input file. This file will contain lines ofcharacters which your program must interpret to draw a maze. Each row of the maze will be describedby a series of numbers and characters, where the numbers before a character tell how many times thatcharacter will be used. If there are multiple digits in a number before a character, then the number oftimes to repeat the character is the sum of the digits before that character.

  The lowercase letter ‘b’ will be used in the input file to represent spaces in the maze. The descriptionsfor different rows in the maze will be separated by an exclamation point (!), or by an end of line.

  Descriptions for different mazes will be separated by a blank line. The input file will be terminatedby an end of file.

Output

For each description in the input file, draw the corresponding maze as it is shown in the sample outputbelow. There is no limit to the number of rows in a maze or the number of mazes in a file, though norow will contain more than 132 characters.

  Print a blank line between two consecutive mazes.

  Happy mazing!

Sample Input

1T1b5T!1T2b1T1b2T!1T1b1T2b2T!1T3b1T1b1T!3T3b1T!1T3b1T1b1T!5T1*1T

11X21b1X

4X1b1X

Sample Output

T TTTTT

T T TT

T T TT

T T T

TTT T

T T T

TTTTT*T

XX X

XXXX X

題意簡述

輸入若干行字串,將其轉換為另外一個字串輸出。字串中包括數字、大小寫字母、"*"、"b"和"!",遇見"!"則輸出換行;遇見連續的數字則將這些數字求和,之後輸出和個數的字母或空格;遇見"b"則輸出空格(若干個);遇見字母則輸出字母(若干個)。

問題分析:(略)

程式說明

  封裝了函式mygets()(函式gets()在新標準中,被建議不要使用,作用就自己做一個),其他都是套路。使用函式putchar()輸出,要比使用函式printf()效率高一些。

AC的C語言程式如下:

/* UVA445 Marvelous Mazes */

#include <stdio.h>
#include <ctype.h>
#include <string.h>

#define MAXN 256

int mygets(char s[])
{
    int i = 0;
    char c;

    while((c = getchar()) && c != '\n' && c != EOF)
        s[i++] = c;
    s[i] = '\0';

    return i > 0 || c != EOF;
}

int main(void)
{
    char s[MAXN];
    int sum, len, i, j;

    while(mygets(s))
    {
        len = strlen(s);
        sum=0;
        for(i=0; i<len; i++) {
            if(isdigit(s[i]))
                sum += s[i] - '0';  /* 數字的話計算輸出個數 */
            else if(s[i] == '!')
                putchar('\n');      /* '!'的話,輸出換行 */
            else {
                /* 如果是字母b,則輸出若干個空格;如果是其他字母怎輸出若干個該字母 */
                for(j=sum; j>0; j--)
                    putchar((s[i] == 'b') ? ' ' : s[i]);
                sum=0;              /* 個數清零 */
            }
        }
        putchar('\n');
    }

    return 0;
}