[UVA] 11482 - Building a Triangular Museum
B |
Building a Triangular Museum
|
You are an architect now; you work for a firm as a head architect. People find you smart and brilliant. You have already showed your talent in some sources. That's why your popularity is growing day by day. And of course who doesn't enjoy such popularity!
Your
firm has recently got a job. The job is quite simple. You have to build a
museum, actually a 'Triangular
Museum'. The shape of the
museum will be triangular and the rooms inside it will be triangular, too. And
the overall building process is given below:
You will be given two numbers M and N. N denotes the number of levels and M denotes the size. That
means you have to build a museum which should have N levels, the first level
will contain one triangular room.
Now, for building a triangular room of size M, first you have to put
/\ (forward slash and a backward slash)
Then for each i-th row (i = 2 to M-1), you have to put a '/' (forward slash) and 2*(i-1) spaces and after that a '\' (backward slash). The M-th row will contain a '/', 2*(M-1) '_' (underscore) and a '\'. And add spaces to the previous parts so that the overall shape looks fully triangular. Suppose, M = 4. Then a room will be like:
.../\
../..\
./....\
/______\
(Here spaces are shown as dots)
Now, for building all the N levels, for each i-th level (i = 2 to N), you have to build i triangular rooms, each will start from the base vertices of the previous triangular rooms. (Note that the first level will contain one room only). If N = 1 and M = 2. Then the museum will be like:
./\
/__\
If N = 2 and M = 2. Then the museum will be like: (Two triangular rooms added)
.../\
../__\
/\../\
/__\/__\
If N = 3 and M = 2. Then the museum will
be like: (Three triangular rooms added)
...../\
..../__\
.../\../\
../__\/__\
./\../\../\
/__\/__\/__\
Now given M and N, your job is to build the triangular museum.
Input
Input
contains several sets of inputs. Each set contains two integers M (1 < M <= 100) and N (1 <= N <= 100). A line containing two zeroes
denotes the end of input.
Output
For
each set of input. First print 'Triangular Museum x' (where x denotes
the set number starting from 1) in a single line. Then draw the
triangular museum according to the description. Finally print a blank line.
Sample Input Output for Sample Input
2 1 |
Triangular Museum 1 Triangular Museum 2 Triangular Museum 3 |
Problem Setter
: Jane Alam Jan
Special Thanks : Sohel Hafiz
這種題目,要慢慢除錯,先試圖完成最左邊的 '/' 以及最右邊的 '\'。
畫出一個大三角形之後,再考慮中間要怎麼思考。
#include <stdio.h>
int main() {
int cases = 0, n, m;
while(scanf("%d %d", &n, &m) == 2) {
if(n == 0 && m == 0)
break;
int i, j, k, l;
int h = n*m-1;
printf("Triangular Museum %d\n", ++cases);
for(i = 0; i < m; i++) {// i level
for(j = 0; j < n; j++) {
for(k = 0; k < h; k++)
putchar(' ');
h--;
for(k = 0; k <= i; k++) {
putchar('/');
for(l = 0; l < 2*j; l++)
putchar(j == n-1 ? '_': ' ');
putchar('\\');
if(k != i) {
for(l = 2; l < 2*n-2*j; l++)
putchar(' ');
}
}
puts("");
}
}
puts("");
}
return 0;
}