Marvelous Mazes
Your mission, if you decide to accept it, is to create a maze drawing program. A maze will consist of the alphabetic charactersA-Z,* (asterisk), and spaces.
Input and Output
Your program will get the information for the mazes from the input file. This file will contain lines of characters which your program must interpret to draw a maze. Each row of the maze will be described by a series of numbers and characters, where the numbers before a character tell how many times that character will be used. If there are multiple digits in a number before a character, then the number of times 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 descriptions for 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 in both input and output. The input file will be terminated by an end of file.
There is no limit to the number of rows in a maze or the number of mazes in a file, though no row will contain more than 132 characters.
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为空格,数字为需要打印的字符或者空格的数目,联系的数字相加,比如11表示2,空行不变的输出。
集体思路:
字符串中除了!前面没有数字以外,其他的都是前面有数字的,所以现在感叹号前面加上1,形成的新的字符串都是数字+字符的形式,然后就逐个进行判定输出。
代码:
#include<iostream> #include<cstdio> #include<string> #include<sstream> using namespace std; int main (){ string str; while(getline(cin,str)){ string des; for(int i=0;i<str.length();i++){ if(str[i]!='!'){ des.push_back(str[i]);//头文件string } else { des.push_back('1'); des.push_back(str[i]); } } // cout<<des<<endl; stringstream ss;//头文件sstream ss<<des; char cm; int cnt=0; while(ss>>cm){ if(cm<='9'&&cm>'0'){ cnt=cm-'0'+cnt; } else{ if(cm=='!'){ printf("\n"); } else if(cm=='b'){ for(int i=0;i<cnt;i++){ printf(" "); } } else { for(int i=0;i<cnt;i++){ printf("%c",cm); } } cnt=0; } } printf("\n"); } return 0; }