【学习笔记】〖九度OJ〗题目1161：Repeater

2020-10-18 05:03发布

Harmony is indispensible in our daily life and no one can live without it----may be Facer is the only exception. One day it is rumored that repeat painting will create harmony and then hundreds of people started their endless drawing. Their paintings were based on a small template and a simple method of duplicating. Though Facer can easily imagine the style of the whole picture, but he cannot find the essential harmony. Now you need to help Facer by showing the picture on computer.
You will be given a template containing only one kind of character and spaces, and the template shows how the endless picture is created----use the characters as basic elements and put them in the right position to form a bigger template, and then repeat and repeat doing that. Here is an example.

# #
#      <-template
# #
So the Level 1 picture will be

# #
#
# #
Level 2 picture will be

# #     # #
#         #
# #     # #
# #
#
# #
# #    # #
#        #
# #    # #

The input contains multiple test cases.
The first line of each case is an integer N, representing the size of the template is N*N (N could only be 3, 4 or 5).
Next N lines describe the template.
The following line contains an integer Q, which is the Scale Level of the picture.
Input is ended with a case of N=0.
It is guaranteed that the size of one picture will not exceed 3000*3000.

For each test case, just print the Level Q picture by using the given template.

```3
# #
#
# #
1
3
# #
#
# #
3
4
OO
O  O
O  O
OO
2
0```

```# #
#
# #
# #   # #         # #   # #
#     #           #     #
# #   # #         # #   # #
# #               # #
#                 #
# #               # #
# #   # #         # #   # #
#     #           #     #
# #   # #         # #   # #
# #   # #
#     #
# #   # #
# #
#
# #
# #   # #
#     #
# #   # #
# #   # #         # #   # #
#     #           #     #
# #   # #         # #   # #
# #               # #
#                 #
# #               # #
# #   # #         # #   # #
#     #           #     #
# #   # #         # #   # #
OO  OO
O  OO  O
O  OO  O
OO  OO
OO          OO
O  O        O  O
O  O        O  O
OO          OO
OO          OO
O  O        O  O
O  O        O  O
OO          OO
OO  OO
O  OO  O
O  OO  O
OO  OO     ```

2011年北京大学计算机研究生机试真题

```#include<iostream>
#include<stdio.h>
#include<cmath>
using namespace std;

char temp[3001][3001];
char res1[3001][3001];
char res2[3001][3001];

void tempcopy(char source[3001][3001], char target[3001][3001], /*target的首坐标*/int r, int c, int n)
{
for (int i=0; i<n; i++)
{
for (int j=0; j<n; j++)
{
target[r+i][c+j] = source[i][j];
}
}
}
//模板，长度，上一层模板，长度，结果，特殊字符，层数
void nextLevel(char temp[3001][3001], int n, char module[3001][3001], char res[3001][3001], char c, int l)
{
//清空RES
int mmax = pow((double)n, (double)l);

int i,j;
for (i=0; i<mmax; i++)
{
for (j=0; j<mmax; j++)
{
res[i][j] = ' ';
}
}
int bias = pow((double)n, (double)l-1);
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
if (temp[i][j] == c)
{
tempcopy(module, res, i*bias, j*bias, bias);
}
}
}
int max = l*n;
for (i=0; i<max; i++)
{
for (i=0; i<max; i++)
{
if (res[i][j] != c)
{
res[i][j] = ' ';
}
}
}
}

int main()
{

int n, q, i, j;

while(cin >> n && n!=0)
{
for (i=0; i<7; i++)
{
for (j=0; j<7; j++)
{
temp[i][j] = ' ';

}
}
char c=' ';
cin.get();
for (i=0; i<n; i++)
{
cin.getline(temp[i],n+1);
}

cin >> q;
int max = pow((double)n,(double)q);
//清空
for (i=0; i<max; i++)
{
for (j=0; j<max; j++)
{
res1[i][j] = ' ';
res2[i][j] = ' ';
}
}
//复制temp用于第一次扩展去特殊化
for (i=0; i<n; i++)
{
for (j=0; j<n; j++)
{
res1[i][j] = temp[i][j];
}
}
//确定tmp中的特殊字符
for (i=0; i<n; i++)
{
if (temp[0][i] != ' ')
{
c = temp[0][i];
break;
}
}

//扩展
for (i=2; i<=q; i++)
{

if (i%2 != 0)//奇次
{
nextLevel(temp, n, res2, res1, c, i);
}
else//偶次
{
nextLevel(temp, n, res1, res2, c, i);
}
}

if (i%2 != 0)
{
for (i=0; i<max; i++)
{

for (j=0; j<max; j++)
{

printf("%c", res2[i][j]);
}
printf("\n");
}

}
else
{
for (i=0; i<max; i++)
{

for (j=0; j<max; j++)
{

printf("%c", res1[i][j]);
}
printf("\n");
}

}

}

return 0;
}```

Ta的文章 更多文章

0条评论